简体   繁体   English

对连续的预测变量进行分类并计算二进制结果的比例

[英]Categorize a continuous predictor variable and calculate proportion of binary outcome

I'm testing for non-linearity for the relationship between different continuous variables and a binary outcome. 我正在测试不同连续变量和二进制结果之间的非线性关系。 I would like a fast and efficient way to plot outcome probability by categorized variable. 我想要一种快速有效的方法来按分类变量绘制结果概率。 Here's what I've got, but it seems clunky: 这是我所拥有的,但似乎很笨拙:

First, data: 一,数据:

(edit: was missing a quotation) (编辑:缺少引号)

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,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, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0)), .Names = c("BMI", "Mortality"), row.names = c(NA, 
-312L), class = "data.frame")

Here's what I've got: 这是我得到的:

df$BMIcut <- cut(df$BMI,breaks = 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) 

Which produces 哪个产生

在此处输入图片说明

There's got to be a faster way..? 必须有一个更快的方法..?

I don't understand why you need to make so many redundant copies to produce this plot: 我不明白为什么您需要制作许多冗余副本才能生成此图:

t <- prop.table(table(df$BMIcut, df$Mortality),1)
plot(x= factor(levels(df$BMIcut)), y= t[,2], ylim=c(0,1))

Though, as noted in the comments, I get different values than your plot. 但是,如评论中所述,我得到的值与您的图不同。 And you only have two obs in the (0,17.5] bucket. Not sure that's a nonlinearity vs data sparsity. 而且(0,17.5]存储桶中只有两个Obs。不确定这是非线性还是数据稀疏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过另一个变量计算几个二进制变量的比例 - Calculate proportion of several binary variables by another variable 可视化连续预测变量与分类结果之间的关系 - Visualizing the relationship between a continuous predictor and a categorical outcome 使用 ggplot2 可视化连续预测变量对二分结果的影响 - Visualising the effect of a continuous predictor on a dichotomous outcome using ggplot2 使用连续预测变量的多个值计算 emmeans - Calculate emmeans using multiple values of the continuous predictor 由线性回归中的每个变量解释的结果方差比例 - Proportion of variance of outcome explained by each variable in a linear regression 变量筛选连续结果,分类预测因子,负p值 - Variable screening continuous outcomes, categorical predictor, negative p-values 如何在R中将相同大小的连续变量分为4组? - How to categorize a continuous variable in 4 groups of the same size in R? 如何在ggplot中显示不同条件下二元结果的变化比例? - How can I show the changing proportion of binary outcome under different conditions in ggplot? 如何绘制图形以检查重要预测变量以何种方式影响我的结果变量? - How can I graph to check in what way a significant predictor is effecting my outcome variable? 模拟与现有二进制变量相关的连续变量 - Simulate continuous variable that is correlated to existing binary variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM