Estimate all possible linear models with two predictors and interaction. Use half your data. That is, regress y
on the intercept only. Then regress y
on x1
. Then regress y
on x2
. Then on x1
and x2
. Then on x1*x2
, then on x1
and x1*x2
, etc. This should result in 8 different models. All models have an intercept, but the first has only the intercept. Note: if you type formula('y~X1*X2')
this will be expanded to 'y~X1+X2+X1*X2'
. You need to use 'y~I(X1*X2)'
to avoid this behavior.
用两个预测因子和相互作用估计所有可能的线性模型。使用您的数据的一半。也就是说,只在截距上回归y。然后在x1上回归y。然后在x2上回归y。然后是x1和x2。然后在' x1x2 '上,然后是' x1 '和' x1x2 '等等,这将导致8个不同的模型。所有的模型都有一个截距,但第一个只有截距。注意:如果你输入“公式(y~X1X2)”,那么它将被扩展到“y~X1+X2+X1X2”。你需要使用“y~I(X1*X2)”来避免这种行为。
请各位大神给几个栗子
之前的data code:
#1.
generate.lm <- function(n, beta, sigma = 1){
if (length(beta) < 1)stop("beta cannot be empty")
if (sigma < 0)stop ("SD not non-negative")
epsilon = rnorm (n, 0, sigma)
X = t(sapply(1:n, function (i)runif (length(beta), -10,10)))
y = X %*% (beta) + epsilon
U = data.frame (y,X)
return(U)
}
#2.
set.seed(0000123456) ## replace with your UID
n = 400
beta = c(5,0)
dat = generate.lm(n, beta)
## more code needed