R中的线性回归模型矩阵

时间:2015-10-28 22:57:15

标签: r regression matrix

考虑模型enter image description here,其中enter image description here

$ X $的矩阵是

enter image description here

如何将此矩阵输入R?

2 个答案:

答案 0 :(得分:2)

假设x = 1,2,...,N,N = 10且y = 2x +ε,ε~N(0,1)那么你会写这样的东西:

N = 10;
set.seed(123)
x =  1:N
e = rnorm(N)  
y = 2*x + e;
mod <- lm( y ~x);
Xmatrix = matrix( c(rep(1,N), x), ncol=2)

请参阅Matrices and matrix computations in R上的以下链接,了解有关此事的更多详情。

答案 1 :(得分:1)

您可以将此矩阵输入R并执行Y~X,或省略截距并执行Y~ $ {X} _i $ -1,其中$ X_i $只是一个向量。