Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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_Regression_Linear Regression_Prediction_Confidence Interval - Fatal编程技术网

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