【发布时间】:2021-02-10 04:35:51
【问题描述】:
我想使用 gmodels 包中的 CrossTable 函数在我的数据集中评估 2 个二元变量种族和性别的卡方统计量,然后使用 corrplot 包可视化残差。这是我在 R 中的代码:
#Loading libraries
library(gmodels)
library(corrplot)
#Dataset
Datavisit1<- read_excel("~/Downloads/Datavisit1.xlsx")
Datavisit1 <- structure(list(PATIENTID = c(1548, 2371, 3843, 9573, 3352, 8590, 6217, 8503, 6610, 2783), DX = c("AS", "AS", "AS", "RA", "AS", "RA", "AS", "RA", "AS", "RA"), AGE = c(22, 74, 18, 22, 59, 45, 33, 20, 32, 60), ETH = c(0, 0, 1, 0, 1, 1, 1, 0, 0, 0), SEX = c(1, 1, 0, 0, 0, 0, 0, 0, 0, 1)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
# replaced the useless call to load an Excel file the we don't have with comment.
#CrossTable
ethsex<- with(Datavisit1, CrossTable(ETH, SEX, prop.r = FALSE, prop.chisq = FALSE,
prop.t = FALSE, chisq = TRUE, resid = TRUE, format = "SPSS"))
#Making Corrplot by extracting residuals from previous step
corrplot(ethsex$residuals, is.corr = FALSE)
我收到错误消息,说我需要矩阵或数据框。有人可以帮助我做错什么吗?本质上,我想得到一个像这样的漂亮图表,但对于 2x2 情况(种族和性别):
提前谢谢你!
【问题讨论】:
-
可以分享
dput(head(Datavisit1, 10))吗?这样我们就可以重现例子并尝试给你答案。 -
猜测是在使用 excel 文件时从
CrossTable()中删除format=SPSS,然后使用as.data.frame(ethsex)将ethsex强制转换为 data.frame -
@AlvaroMartinez 您的第一条评论的答案是:structure(list(PATIENTID = c(1548, 2371, 3843, 9573, 3352, 8590, 6217, 8503, 6610, 2783), DX = c (“AS”、“AS”、“AS”、“RA”、“AS”、“RA”、“AS”、“RA”、“AS”、“RA”),AGE = c(22, 74, 18, 22, 59, 45, 33, 20, 32, 60), ETH = c(0, 0, 1, 0, 1, 1, 1, 0, 0, 0), SEX = c(1, 1, 0, 0, 0, 0, 0, 0, 0, 1)), row.names = c(NA, -10L), class= c("tbl_df", "tbl", "data.frame"))
-
对于
ethsex$residuals,我得到NULL。从`str(ethsex)`我同样得到NULL。你确定 yoiu 应该使用CrossTable来创建一个对象以供后续工作?如果您想要交叉制表,为什么不使用table? -
@IRTFM 我也得到了 NULL。我不认为我从 CrossTable 函数中提取残差的代码是正确的......
标签: r chi-squared r-corrplot