在 svychisq 上使用 lapply 函数
Using lapply function on svychisq
我有一个名为“drugs”的数据集,我创建了一个名为“weighted_data”的加权数据集。
weighted_data <- svydesign(id = ~cluster,
strata = ~strata,
weights = ~weights,
data = drugs,
nest = TRUE)
我可以使用此函数对所有变量执行卡方检验,看看有医用大麻法律的州的比例是否与没有医用大麻法律的州有显着差异。
svychisq(~religion_importance + medmj_law, design=weighted_data)
当然,我不想对所有 12 个变量都这样做,所以我想使用 lapply 函数。
lapply(names(drugs[-c(1, 14:16)]), function(x)
svychisq(
as.formula(bquote(~.(as.name(x))) + medmj_law ),
design = weighted_data))
但我一直收到 "Error in as.formula(bquote(~.(as.name(x))) + medmj_law) : object 'medmj_law' 未找到。我的语法一定有问题。
作为参考,第一个变量是“medmj_law”,变量 2-13 是我要对其进行卡方检验的 12 个分类变量。我用这段代码计算了比例,效果很好。
overall_table_weighted <- lapply(names(drugs[,-c(14:16)]), function(x)
svytable(bquote(~.(as.name(x))), design=weighted_data))
overall_prop_weighted <- lapply(names(drugs[,-c(14:16)]), function(x)
round(prop.table(svytable(bquote(~.(as.name(x))), design=weighted_data))*100, 2))
这很简单,只需执行以下操作:
lapply(names(drugs[-c(1, 14:16)]), function(x)
svychisq(
bquote(~.(as.name(x))+ medmj_law),
design = weighted_data))
不客气。
我从@benimwolfspelz的评论中得到了答案:
Though there are other methods (I guess), as.formula()
works well with a string as its first argument. In your case, as.formula
does not try to add the string "medmj_law"
to the formula but instead looks for an object with this name. But you don't have that object in the environment of the anonymous function that you pass to lapply
. Try this: as.formula(paste("~", x, " + medmj_law"))
完整代码:
#Chi-Squared Tests
chisq_weighted <- lapply(names(drugs[-c(1, 14:16)]), function(x)
svychisq(as.formula(paste("~", x, " + medmj_law")), design = weighted_data))
pvalues_weighted <- lapply(names(drugs[-c(1, 14:16)]), function(x)
svychisq(as.formula(paste("~", x, " + medmj_law")), design = weighted_data)$p.value)
谢谢@benimwolfspelz!!
我有一个名为“drugs”的数据集,我创建了一个名为“weighted_data”的加权数据集。
weighted_data <- svydesign(id = ~cluster,
strata = ~strata,
weights = ~weights,
data = drugs,
nest = TRUE)
我可以使用此函数对所有变量执行卡方检验,看看有医用大麻法律的州的比例是否与没有医用大麻法律的州有显着差异。
svychisq(~religion_importance + medmj_law, design=weighted_data)
当然,我不想对所有 12 个变量都这样做,所以我想使用 lapply 函数。
lapply(names(drugs[-c(1, 14:16)]), function(x)
svychisq(
as.formula(bquote(~.(as.name(x))) + medmj_law ),
design = weighted_data))
但我一直收到 "Error in as.formula(bquote(~.(as.name(x))) + medmj_law) : object 'medmj_law' 未找到。我的语法一定有问题。
作为参考,第一个变量是“medmj_law”,变量 2-13 是我要对其进行卡方检验的 12 个分类变量。我用这段代码计算了比例,效果很好。
overall_table_weighted <- lapply(names(drugs[,-c(14:16)]), function(x)
svytable(bquote(~.(as.name(x))), design=weighted_data))
overall_prop_weighted <- lapply(names(drugs[,-c(14:16)]), function(x)
round(prop.table(svytable(bquote(~.(as.name(x))), design=weighted_data))*100, 2))
这很简单,只需执行以下操作:
lapply(names(drugs[-c(1, 14:16)]), function(x)
svychisq(
bquote(~.(as.name(x))+ medmj_law),
design = weighted_data))
不客气。
我从@benimwolfspelz的评论中得到了答案:
Though there are other methods (I guess),
as.formula()
works well with a string as its first argument. In your case,as.formula
does not try to add the string"medmj_law"
to the formula but instead looks for an object with this name. But you don't have that object in the environment of the anonymous function that you pass tolapply
. Try this:as.formula(paste("~", x, " + medmj_law"))
完整代码:
#Chi-Squared Tests
chisq_weighted <- lapply(names(drugs[-c(1, 14:16)]), function(x)
svychisq(as.formula(paste("~", x, " + medmj_law")), design = weighted_data))
pvalues_weighted <- lapply(names(drugs[-c(1, 14:16)]), function(x)
svychisq(as.formula(paste("~", x, " + medmj_law")), design = weighted_data)$p.value)
谢谢@benimwolfspelz!!