将加权卡方检验的一些代码转换为变量的函数

发布于 2025-01-25 14:57:12 字数 1985 浏览 0 评论 0原文

我有一个加权卡方测试的代码。它还需要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:
enter image description here

Do you have any idea on what I should do about get(Var) so that this code works?

Thanks in advance,

Stephanie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2025-02-01 14:57:12

我怀疑您不需要在公式中使用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> form代替svychisq()的公式输入。

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文