将加权卡方检验的一些代码转换为变量的函数
我有一个加权卡方测试的代码。它还需要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)
不幸的是,代码不从Pvalue起作用。当我调用“ get(var)”时,用函数“ svychisq”创建。我收到此错误消息:
您对我应该做什么有任何想法(var),以便此代码有效吗?
预先感谢
斯蒂芬妮
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您不需要在公式中使用
get(var)
,您只需使用字符串var
:即可放置
form> form
代替svychisq()
的公式输入。I suspect you don't need to use
get(Var)
in the formula, you can just use the stringVar
:And you put
form
in place of the formula input tosvychisq()
.