I want to perform a Levene and one-way ANOVA test, with the outcome being continuous and a predictor with n groups. For example:
import pandas as pd
import scipy.stats as sps
# create the df
test_df = pd.DataFrame({
'score': [5,15,1,51,1,51,5,15,16,3,1,81,6,1,2],
'groups': ['a','b','c','a','b','c','a','b','c','a','b','c','a','b','c']})
factor = test_df['groups'].unique() # create a list of individual factors
arr1 = [] # to store the outcome by groups
for i in range(len(factor)):
variable = test_df[test_df['groups'] == factor[i]]['score']
# Make individual values by group
arr1.append(variable) # append these to arr1
levene = sps.levene(arr1) # the problem is here. The code does not recognize my arr1 has multiple arrays in it.
Ideally, I would like to be able to do something like this sps.levene(arr1[0],...,arr1[n]), the loop would do the following:
- append each array within
arr1into a list - perform one
sps.levenetest with the list.
Can someone please help me how to solve this issue?
>Solution :
You need to unpack the elements of arr1 when passing them to levene.
You can achieve this by using the * operator:
import pandas as pd
import scipy.stats as sps
# create the df
test_df = pd.DataFrame({
'score': [5,15,1,51,1,51,5,15,16,3,1,81,6,1,2],
'groups': ['a','b','c','a','b','c','a','b','c','a','b','c','a','b','c']})
factor = test_df['groups'].unique() # create a list of individual factors
arr1 = [] # to store the outcome by groups
for i in range(len(factor)):
variable = test_df[test_df['groups'] == factor[i]]['score']
arr1.append(variable) # append these to arr1
# Perform Levene's test
levene_result = sps.levene(*arr1)
print("Levene's test p-value:", levene_result.pvalue)
Output:
Levene's test p-value: 0.253368701021017