R 中具有多个虚拟编码预测变量的线性回归模型的箱线图
Boxplot of Linear Regression Model with several Dummy coded predictors in R
我有以下线性模型:
model <- lm(var01 ~ a0 + a1 + a2 + a3 + a4 + a5,NT)
其中 var01 是 0-100 的区间标度变量,a0-a5 是虚拟编码 (0, 1) 变量。摘要(模型)给出了这个:
Residuals:
Min 1Q Median 3Q Max
-75.951 -13.469 -7.239 18.795 80.531
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 59.6015 8.7076 6.845 5.48e-10 ***
a01 -46.1329 8.6302 -5.345 5.37e-07 ***
a11 -0.8744 9.0549 -0.097 0.9233
a21 22.0408 9.1278 2.415 0.0175 *
a31 9.5488 9.9284 0.962 0.3384
a41 14.9227 7.6762 1.944 0.0546 .
a51 -8.1222 11.8530 -0.685 0.4947
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 32.13 on 104 degrees of freedom
Multiple R-squared: 0.4393, Adjusted R-squared: 0.407
F-statistic: 13.58 on 6 and 104 DF, p-value: 2.486e-11
我想创建一个箱线图,其中 a0-a5 彼此相邻显示,但只有 a0==1、a1==1 等
所以我尝试了:
ggplot(NT, aes(factor(a0), var01)) +
geom_boxplot() +
geom_smooth(method = "lm", se=FALSE, color="black", aes(group=1))
但这显示了 a0 == 0 和 a0 == 1 的箱线图彼此相邻。所以有两个问题:如何让 R 只显示 a0 == 1?此外,在同一张图中,a0 旁边的所有其他四个预测变量 a1-a5(但也仅限于 a1-a4 == 1)?
非常感谢您的帮助。谢谢:)
更新:示例数据
id category_a var01 a0 a1 a2 a3 a4 a5
3 1;5 100 0 1 0 0 0 1
4 1;5 0 0 1 0 0 0 1
5 0 21 1 0 0 0 0 0
6 1;2;4 100 0 1 1 0 1 0
9 1;2 68 0 1 1 0 0 0
所以a0-a5是多类变量的虚拟编码"category_a"。
这是数据重塑的问题。如果您感兴趣的每个数据点都是数据帧中的一行(长格式),则 ggplot 效果最佳。
library(ggplot2)
library(reshape2)
#generate data
set.seed(1)
n=1000
NT <- data.frame(id=1:n,
var01=rnorm(n),
a0=rbinom(n,1,0.2),
a1=rbinom(n,1,0.2),
a2=rbinom(n,1,0.2),
a3=rbinom(n,1,0.2),
a4=rbinom(n,1,0.2),
a5=rbinom(n,1,0.2))
#do some data-reshaping before plotting
#ggplot needs each data-point on one line
#so transform to long
plotdata <- melt(NT,id.vars=c("id","var01"),variable.name="a")
现在绘制所有内容非常容易:
#plot everything using interaction
p1 <- ggplot(plotdata, aes(x=interaction(a,value), y=var01)) +
geom_boxplot()
p1
或选择:
p2 <- ggplot(plotdata[plotdata$value==1,],
aes(x=a, y=var01)) +
geom_boxplot()
p2
我有以下线性模型:
model <- lm(var01 ~ a0 + a1 + a2 + a3 + a4 + a5,NT)
其中 var01 是 0-100 的区间标度变量,a0-a5 是虚拟编码 (0, 1) 变量。摘要(模型)给出了这个:
Residuals:
Min 1Q Median 3Q Max
-75.951 -13.469 -7.239 18.795 80.531
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 59.6015 8.7076 6.845 5.48e-10 ***
a01 -46.1329 8.6302 -5.345 5.37e-07 ***
a11 -0.8744 9.0549 -0.097 0.9233
a21 22.0408 9.1278 2.415 0.0175 *
a31 9.5488 9.9284 0.962 0.3384
a41 14.9227 7.6762 1.944 0.0546 .
a51 -8.1222 11.8530 -0.685 0.4947
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 32.13 on 104 degrees of freedom
Multiple R-squared: 0.4393, Adjusted R-squared: 0.407
F-statistic: 13.58 on 6 and 104 DF, p-value: 2.486e-11
我想创建一个箱线图,其中 a0-a5 彼此相邻显示,但只有 a0==1、a1==1 等
所以我尝试了:
ggplot(NT, aes(factor(a0), var01)) +
geom_boxplot() +
geom_smooth(method = "lm", se=FALSE, color="black", aes(group=1))
但这显示了 a0 == 0 和 a0 == 1 的箱线图彼此相邻。所以有两个问题:如何让 R 只显示 a0 == 1?此外,在同一张图中,a0 旁边的所有其他四个预测变量 a1-a5(但也仅限于 a1-a4 == 1)?
非常感谢您的帮助。谢谢:)
更新:示例数据
id category_a var01 a0 a1 a2 a3 a4 a5
3 1;5 100 0 1 0 0 0 1
4 1;5 0 0 1 0 0 0 1
5 0 21 1 0 0 0 0 0
6 1;2;4 100 0 1 1 0 1 0
9 1;2 68 0 1 1 0 0 0
所以a0-a5是多类变量的虚拟编码"category_a"。
这是数据重塑的问题。如果您感兴趣的每个数据点都是数据帧中的一行(长格式),则 ggplot 效果最佳。
library(ggplot2)
library(reshape2)
#generate data
set.seed(1)
n=1000
NT <- data.frame(id=1:n,
var01=rnorm(n),
a0=rbinom(n,1,0.2),
a1=rbinom(n,1,0.2),
a2=rbinom(n,1,0.2),
a3=rbinom(n,1,0.2),
a4=rbinom(n,1,0.2),
a5=rbinom(n,1,0.2))
#do some data-reshaping before plotting
#ggplot needs each data-point on one line
#so transform to long
plotdata <- melt(NT,id.vars=c("id","var01"),variable.name="a")
现在绘制所有内容非常容易:
#plot everything using interaction
p1 <- ggplot(plotdata, aes(x=interaction(a,value), y=var01)) +
geom_boxplot()
p1
或选择:
p2 <- ggplot(plotdata[plotdata$value==1,],
aes(x=a, y=var01)) +
geom_boxplot()
p2