Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何通过调整协变量等处理来绘制预测因子。?_R_Ggplot2_Prediction - Fatal编程技术网

R 如何通过调整协变量等处理来绘制预测因子。?

R 如何通过调整协变量等处理来绘制预测因子。?,r,ggplot2,prediction,R,Ggplot2,Prediction,我想用三次预测因子和大量调整后的协变量和相互作用来描绘拟合的治疗效果。使用ggplot我可以轻松地通过处理对数据进行分组,并添加geom_smooth()以获得此结果,而无需调整。我应用了我的问题,但当您的模型中有大量调整时,这是一种痛苦,而当您有长格式的数据时,这几乎不适用。所以我的问题是,是否有一个更容易的方法来得到我想要的 一些数据 set.seed(42) n <- 1e4 D <- rbinom(n, 1, .5) # treatment indicator X <

我想用三次预测因子和大量调整后的协变量和相互作用来描绘拟合的治疗效果。使用
ggplot
我可以轻松地通过处理对数据进行分组,并添加
geom_smooth()
以获得此结果,而无需调整。我应用了我的问题,但当您的模型中有大量调整时,这是一种痛苦,而当您有长格式的数据时,这几乎不适用。所以我的问题是,是否有一个更容易的方法来得到我想要的

一些数据

set.seed(42)
n <- 1e4
D <- rbinom(n, 1, .5)  # treatment indicator
X <- .5 + rnorm(n)  # bunch of covariates and other adjustemnts
P <- 5.54 + 0.35*D -.24*X + rnorm(n)  # predictor
Y <- 1.49 - 1.35*P + .5*P^2 - 0.04*P^3 - 0.83*D + 0.43*X + rnorm(n, 0, 6)
df1 <- data.frame(D, X, P, Y)

把这个方法应用到我的问题上,我得到了以下几点

预测

n.data <- data.frame(D=rep(range(D), each=n/2),
                   P=rep(seq(range(df1$P)[1], range(df1$P)[2],
                              length.out=n/2), times=2),
                   X=rep(seq(range(df1$X)[1], range(df1$X)[2],  # assume this dozens of!
                             length.out=n/2), times=2))
df1.2 <- data.frame(n.data, pred=predict(true, n.data))

n.data我知道这个问题很老,所以仅供参考

我会选择
效果
套餐:

set.seed(42)
n <- 1e4
D <- rbinom(n, 1, .5)  # treatment indicator
X <- .5 + rnorm(n)  # bunch of covariates and other adjustemnts
P <- 5.54 + 0.35*D -.24*X + rnorm(n)  # predictor
Y <- 1.49 - 1.35*P + .5*P^2 - 0.04*P^3 - 0.83*D + 0.43*X + rnorm(n, 0, 6)
df1 <- data.frame(D = factor(D, labels = c("Control", "Treatment")), X, P, Y)
true <- lm(Y ~ poly(P, 3, raw = TRUE):D + X , df1)  # true model
library(effects)
plot(predictorEffect("P", true), lines=list(multiline=TRUE))
set.seed(42)

实际上,在这期间我停止使用ggplot,这看起来很棒+1!
n.data <- data.frame(D=rep(range(D), each=n/2),
                   P=rep(seq(range(df1$P)[1], range(df1$P)[2],
                              length.out=n/2), times=2),
                   X=rep(seq(range(df1$X)[1], range(df1$X)[2],  # assume this dozens of!
                             length.out=n/2), times=2))
df1.2 <- data.frame(n.data, pred=predict(true, n.data))
p1a <- ggplot(df1, aes(x=P, y=Y, color=as.factor(D)))  +
  geom_smooth(data=df1.2, aes(x=P, y=pred, color=as.factor(D))) +
  theme_bw()
p2a <- ggplot(df1, aes(x=P, y=Y, color=D)) +
  stat_smooth(method="lm", formula=y ~ poly(x, 3, raw=TRUE),
              data=df1.2, aes(x=P, y=pred, color=as.factor(D))) +
  theme_bw()
egg::ggarrange(p1a, p2a)
set.seed(42)
n <- 1e4
D <- rbinom(n, 1, .5)  # treatment indicator
X <- .5 + rnorm(n)  # bunch of covariates and other adjustemnts
P <- 5.54 + 0.35*D -.24*X + rnorm(n)  # predictor
Y <- 1.49 - 1.35*P + .5*P^2 - 0.04*P^3 - 0.83*D + 0.43*X + rnorm(n, 0, 6)
df1 <- data.frame(D = factor(D, labels = c("Control", "Treatment")), X, P, Y)
true <- lm(Y ~ poly(P, 3, raw = TRUE):D + X , df1)  # true model
library(effects)
plot(predictorEffect("P", true), lines=list(multiline=TRUE))