/ 猿问

# R ,估计可能的线性模型

2018-02-23 02:07:19

3. 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.

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.

#1.
```{r generate-function, echo=TRUE}
generate.lm <- function(n, beta, sigma = 1){
if (length(beta) < 1)stop("beta is empty")
if (sigma < 0)stop ("sigma should not be 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.
```{r creating-data}
set.seed(0003579862) ## replace with your UID
n = 400
beta = c(5,0)
dat = generate.lm(n, beta)
## mo

• 0 回答
• 0 关注
• 755 浏览

0/150