在函数内: 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 (将#修改为@)