NA coefficient in linear regression R -
i have run simulation r.
basically have create set of variables (x) in matrix nxp first variable has value 1, , other 23 variables has random values randomly extracted n(0,1). vector beta of length 24 has first 2 values 1 , rest 0. vector epsilon of length 24 extracted n(0,1). after create variable y is: y=x %*% beta + epsilon. select variable of x has max(cor(abs(xj,y))) j goes (3,24) , have run model y ~ x1 + x2 + xj , see results.
> set.seed(123) > > n=25 p=24 b=seq(1,1000) > > x <- cbind(matrix(1,nrow=25,ncol=1),matrix(rnorm(25*23),nrow=25, > ncol=23)) beta <- t(t(c(1,1,rep(0,22)))) eps <- t(t(rnorm(25))) > > y <- x %*% beta + eps > > j<-seq(3,24) m <- which.max(abs(cor(x[,j],y))) > > newx <- as.data.frame(cbind(y,x[,1], x[,2], x[,m+2])) anyna(newx[,2]) > mod <- lm(v1 ~ . , data=newx) > summary(mod) call: lm(formula = v1 ~ ., data = newx) residuals: min 1q median 3q max -1.42575 -0.90957 0.06547 0.38879 2.39707 coefficients: (1 not defined because of singularities) estimate std. error t value pr(>|t|) (intercept) 1.1235 0.2421 4.641 0.000126 *** v2 na na na na v3 0.6803 0.2775 2.452 0.022612 * v4 -0.5943 0.3036 -1.957 0.063101 . --- signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 residual standard error: 1.085 on 22 degrees of freedom multiple r-squared: 0.3958, adjusted r-squared: 0.3408 f-statistic: 7.205 on 2 , 22 df, p-value: 0.003919
everything works fine, can see estimated coefficient v2, coefficient variable x1, composed 1, na. don't understand why have na result, values in variable listed numeric , there's no missing value.
if can me understand, thanks!
Comments
Post a Comment