如何解决在函数内: chisq.test(tbl) 中的错误:至少一个“x”条目必须为正
我正在尝试查看许多分类变量之间的相互作用。我正在尝试收集 P 值。我发现代码 here 似乎与我要执行的操作类似,但我似乎无法让代码正确处理我的数据。
我梳理数据以首先查看变量并列出每个可能的变量组合。但是,当我尝试使用该函数并将其应用于每个变量组合时,它会在代码的 chisq.test 部分引发错误,因此永远不会创建 df_res。
catOnly.test 包含类似的数据(除最后两个之外的所有类别)。
有什么建议吗?
V_NP_001 001 FU12Month V N 0 4 None 0.000 1.734
f = function(x,y) {
tbl = as.data.frame(catOnly.test)%>% select(x,y) %>% table()
chisq_pval = round(chisq.test(tbl)$p.value,4)
cramV = round(cramersV(tbl),4)
data.frame(x,y,chisq_pval,cramV) }
# create unique combinations of column names
# sorting will help getting a better plot (upper triangular)
df_comb = data.frame(t(combn(sort(names(catOnly.test)),4)),stringsAsFactors = F)
# apply function to each variable combination
df_res = map2_df(df_comb$X1,df_comb$X2,f)
# plot results
df_res %>%
ggplot(aes(x,fill=chisq_pval))+
geom_tile()+
geom_text(aes(x,label=cramV))+
scale_fill_gradient(low="red",high="yellow")+
theme_classic()
print(df_res)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。