我测试不同连续变量和二元结果之间关系的非线性。我想通过分类变量快速有效地绘制结果概率。这就是我所拥有的,但它看起来很笨重:
首先,数据:
(编辑:缺少报价单)
df <- structure(list(BMI = c(23, 23, 19, 21, 24, 25, 22, 20,
20, 18, 18, 22, 23, 22, 20, 21, 20, 23, 26, 18, 20, 25, 28, 21,
24, 21, 21, 19, 22, 19, 21, 27, 21, 20, 20, 20, 22, 25, 20, 24,
25, 31, 27, 22, 21, 26, 23, 24, 31, 22, 22, 25, 24, 20, 23, 19,
20, 24, 20, 22, 23, 21, 20, 22, 21, 22, 21, 25, 20, 31, 23, 22,
24, 25, 23, 28, 20, 28, 20, 23, 27, 22, 21, 20, 25, 22, 28, 25,
27, 27, 29, 21, 21, 24, 25, 24, 22, 29, 23, 34, 22, 27, 18, 25,
23, 26, 23, 23, 21, 22, 29, 26, 23, 23, 21, 21, 24, 20, 21, 23,
27, 24, 31, 25, 19, 21, 21, 23, 19, 21, 22, 26, 21, 22, 22, 23,
25, 19, 20, 21, 20, 22, 20, 21, 26, 20, 22, 24, 21, 24, 22, 24,
28, 22, 24, 25, 30, 20, 24, 29, 23, 24, 24, 22, 20, 21, 22, 25,
19, 25, 20, 23, 25, 24, 17, 26, 25, 20, 21, 20, 22, 5, 26, 25,
26, 20, 23, 20, 19, 25, 21, 37, 20, 28, 32, 22, 23, 26, 23, 21,
24, 20, 22, 19, 24, 22, 22, 25, 24, 26, 25, 21, 21, 22, 27, 27,
24, 24, 25, 26, 18, 21, 28, 25, 21, 22, 21, 19, 24, 21, 25, 23,
21, 24, 22, 25, 23, 26, 23, 23, 21, 22, 25, 19, 24, 20, 26, 29,
19, 22, 24, 30, 28, 24, 31, 22, 27, 25, 23, 23, 26, 23, 25, 23,
24, 29, 23, 23, 26, 24, 32, 31, 22, 31, 22, 21, 18, 24, 21, 25,
25, 22, 24, 28, 22, 23, 22, 24, 32, 28, 26, 27, 22, 20, 23, 18,
20, 20, 19, 30, 28, 27, 29, 23, 20, 20, 25, 28, 22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aks = c(0,17.5,20,22.5,25,30))
df$MortBMIcut <- NULL
for(i in levels(df$BMIcut)){
df[which(df$BMIcut==i & is.na(df$BMIcut)==F),"MortBMIcut"] <-
sum(df[which(df$BMIcut==i & is.na(df$BMIcut)==F & is.na(df$Mortality)==F),"Mortality"])/
NROW(df[which(df$BMIcut==i & is.na(df$BMIcut)==F & is.na(df$Mortality)==F),"Mortality"])
}
plot(MortBMIcut ~ BMIcut, data=df)
哪个产生
必须有更快的方式......?
答案 0 :(得分:1)
我不明白为什么你需要制作这么多多余的副本来制作这个剧情:
t <- prop.table(table(df$BMIcut, df$Mortality),1)
plot(x= factor(levels(df$BMIcut)), y= t[,2], ylim=c(0,1))
尽管如评论所述,我得到的价值与你的情节不同。并且你在(0,17.5)桶中只有两个obs。不确定这是非线性与数据稀疏性。