R 在每组的中心找到回归线的置信区间
我有以下模拟数据来拟合回归模型,R 在每组的中心找到回归线的置信区间,r,regression,linear-regression,prediction,confidence-interval,R,Regression,Linear Regression,Prediction,Confidence Interval,我有以下模拟数据来拟合回归模型,y,x1是连续变量,x2是分类变量 y <- rnorm(100, 2, 3) x1 <- rnorm(100, 2.5, 2.8) x2 <- factor(c(rep(1,45), rep(0,55))) 我想知道我这样做是否正确。我还想知道是否有比这更简单的方法。设置 set.seed(0) y <- rnorm(100, 2, 3) x1 <- rnorm(100, 2.5, 2.8) x2 <- factor(c(r
y
,x1
是连续变量,x2
是分类变量
y <- rnorm(100, 2, 3)
x1 <- rnorm(100, 2.5, 2.8)
x2 <- factor(c(rep(1,45), rep(0,55)))
我想知道我这样做是否正确。我还想知道是否有比这更简单的方法。设置
set.seed(0)
y <- rnorm(100, 2, 3)
x1 <- rnorm(100, 2.5, 2.8)
x2 <- factor(c(rep(1,45), rep(0,55)))
mod <- lm(y ~ x1 * x2)
或者你可以
pred.data <- setNames(stack(tapply(x1, x2, mean)), c("x1", "x2"))
# x1 x2
#1 2.649924 0
#2 2.033328 1
pred.data
pred.data <- data.frame(x1 = mean(x1[x2 == "0"]), x2 = "0")
# x1 x2
#1 2.649924 0
pred.data <- setNames(stack(tapply(x1, x2, mean)), c("x1", "x2"))
# x1 x2
#1 2.649924 0
#2 2.033328 1