I have a code that does weighted chi-square tests. It also takes the p-value and add it into a dataset. I would like to transform the code into a function where the arguments is the variable names. The data (called) looks something like that:
Gender UserStatus Weight
Female HC 1.3
Male IDP 0.8
Male HC 1.3
Male V 1.1
Female IDP 0.8
Female HC 1.3
Female IDP 0.8
Male HC 1.3
Female IDP 0.8
Male IDP 0.8
Male IDP 0.8
Female IDP 0.8
Female V 1.1
Male IDP 0.8
Male V 1.1
Female HC 1.3
Male IDP 0.8
Male V 1.1
....
The code without the function looks like this:
#Create the empty database
dTests <- data.frame(matrix(ncol = 3, nrow = 0))
colnames(dTests) <- c('var1', 'var2', 'pvalue')
#Affiliate the weight to the data
dGenderUserStatus <- data %>%
filter(!is.na(Gender) & !is.na(UserStatus)) %>%
as_survey_design(weights = Weight)
#Calculate the Chi square p-value
pvalue <- svychisq( ~ Gender UserStatus, dGenderUserStatus) %>%
.$p.value
#add row
dTests[nrow(dTests) 1,] <- c("Gender","UserStatus", pvalue)
The function - that I attempted to write and for which it does not work - looks something like that:
#Create the empty database
dTests <- data.frame(matrix(ncol = 3, nrow = 0))
colnames(dTests) <- c('var1', 'var2', 'pvalue')
FctChiSquareTestGender <- function(data,Var,weight){
dGenderVar <- data %>%
filter(!is.na(Gender) & !is.na(get(Var))) %>%
as_survey_design(weights = {{weight}})
pvalue <- svychisq( ~ Gender get(Var), dGenderVar) %>%
.$p.value
#add row
dTests[nrow(dTests) 1,] <- c("Gender",Var, pvalue)
}
FctChiSquareTestGender(data,"UserStatus",Weight)
Unfortunately, the code does not work from the pvalue creation with the function "svychisq" when I call "get(Var)". I get this error message:
Do you have any idea on what I should do about get(Var) so that this code works?
Thanks in advance,
Stephanie
CodePudding user response:
I suspect you don't need to use get(Var)
in the formula, you can just use the string Var
:
Gender <- rbinom(100, 1, 0.5)
statuses <- c("HC", "IDP", "V")
Userstatus <- replicate(sample(statuses, 1), n = 100)
myfunction <- function(Var){
form <- as.formula(paste("~ Gender", Var, sep = " "))
form
}
myfunction("Userstatus")
#> ~Gender Userstatus
#> <environment: 0x55ba8badbbf0>
And you put form
in place of the formula input to svychisq()
.