r - Linear Regression Coefficient Information as Data Frame or Matrix -
i trying create script optimize linear regression analysis, , operate on model output, pr(>|t|) value. unfortunately, not know how model output matrix or data table.
here example: in code below, create 7 columns of data, , fit seventh using other six. when summary of model, clear 3 of parameters more significant than other three. if had access coefficient output numerically, perhaps create script drop least significant parameter , re-run analysis... is, doing manually.
what best way this?
thank in advance help.
q = matrix( c(2,14,-4,1,10,9,41,8,13,2,0,20,3,27,1,10,-1,0, 10,-6,23,6,13,-8,1,15,-7,55,7,14,10,0,20,-3,6,4,20, -1,5,19,-2,48,10,19,8,8,10,-2,24,8,13,9,8,14,5,7,7, 12,1,0,16,7,27,7,10,-1,1,15,7,31,2,20,-5,10,12,3,57, 0,19,-8,8,11,-4,63,5,11,7,8,10,-7,6,9,10,-7,2,19,8, 51,2,18,3,3,14,4,30), nrow=15, ncol=7, byrow = true) # colnames(q) <- c("a","b","c","d","e","f","z") # q <- as.data.frame(q) # qmodel <- lm(z~.,data=q) # summary(qmodel) # output:
call: lm(formula = z ~ ., data = q) residuals: min 1q median 3q max -1.25098 -0.52655 -0.02931 0.62350 1.26649 coefficients: estimate std. error t value pr(>|t|) (intercept) -2.09303 1.51627 -1.380 0.205 0.91161 0.11719 7.779 5.34e-05 *** b 1.99503 0.09539 20.914 2.87e-08 *** c -2.98252 0.04789 -62.283 4.91e-12 *** d 0.13458 0.10377 1.297 0.231 e 0.15191 0.09397 1.617 0.145 f 0.01417 0.04716 0.300 0.772 --- signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 residual standard error: 0.9439 on 8 degrees of freedom multiple r-squared: 0.9986, adjusted r-squared: 0.9975 f-statistic: 928.9 on 6 , 8 df, p-value: 6.317e-11 now here i'd like see:
> coeffs estimate std. error t value pr(>|t|) (intercept) -2.09303 1.51627 -1.380 2.05e-01 0.91161 0.11719 7.779 5.34e-05 b 1.99503 0.09539 20.914 2.87e-08 c -2.98252 0.04789 -62.283 4.91e-12 d 0.13458 0.10377 1.297 2.31e-01 e 0.15191 0.09397 1.617 1.45e-01 f 0.01417 0.04716 0.300 7.72e-01 as is, got in manner... not automated @ all...
coeffs = matrix( c(-2.09303,1.51627,-1.38,0.205,0.91161,0.11719, 7.779,0.0000534,1.99503,0.09539,20.914,0.0000000287, -2.98252,0.04789,-62.283,0.00000000000491,0.13458, 0.10377,1.297,0.231,0.15191,0.09397,1.617,0.145, 0.01417,0.04716,0.3,0.772), nrow=7, ncol=4, byrow = true) # rownames(coeffs) <- c("(intercept)","a","b","c","d","e","f") colnames(coeffs) <- c("estimate","std. error","t value","pr(>|t|)") # coeffs <- as.data.frame(coeffs) # coeffs
what want coefficients component of summary object.
m <- lm(z~.,data=q) summary(m)$coefficients some further comments:
- use
stepstepwise variable selection rather coding yourself; - stepwise variable selection has bad statistical properties; consider
glmnet(in package of same name) regularized model building instead.
Comments
Post a Comment