将加权卡方检验的一些代码转换为变量函数
Transforming some codes on weighted chi-square tests into functions of variables
我有一个代码可以进行加权卡方检验。它还获取 p 值并将其添加到数据集中。我想将代码转换为一个函数,其中参数是变量名。数据(称为)看起来像这样:
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
....
没有函数的代码如下所示:
#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)
我尝试编写但它不起作用的函数看起来像这样:
#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)
不幸的是,当我调用“get(Var)”时,代码无法通过函数“svychisq”创建 pvalue。我收到此错误消息:
您是否知道我应该如何处理 get(Var) 以便此代码有效?
提前致谢,
斯蒂芬妮
我怀疑你不需要在公式中使用 get(Var)
,你可以只使用字符串 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>
你用 form
代替 svychisq()
的公式输入。
我有一个代码可以进行加权卡方检验。它还获取 p 值并将其添加到数据集中。我想将代码转换为一个函数,其中参数是变量名。数据(称为)看起来像这样:
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
....
没有函数的代码如下所示:
#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)
我尝试编写但它不起作用的函数看起来像这样:
#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)
不幸的是,当我调用“get(Var)”时,代码无法通过函数“svychisq”创建 pvalue。我收到此错误消息:
您是否知道我应该如何处理 get(Var) 以便此代码有效?
提前致谢,
斯蒂芬妮
我怀疑你不需要在公式中使用 get(Var)
,你可以只使用字符串 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>
你用 form
代替 svychisq()
的公式输入。