R: Hide dummies output

我怕爱的太早我们不能终老 提交于 2019-12-08 07:21:01

问题


I'm new to running regressions with R. Learning by doing and looking at different online tutorials, here's what I'm doing atm to regress y onto x1 and have dummies for x2 and x3 (but no interacted dummies):

myDataTable[, x2.f := factor(x2)]
myDataTable[, x3.f := factor(x3)]
ols <- myDataTable[, lm(y ~ x1 + x2.f +x3.f)]

Now, I would like to look at my regression output, but it's very long, since there's many (think thousands) of values for x3, summary(ols) is unreadable.

How can I look at the regression output, hiding the output for the two factor variables? This should be quite standard, but none of the arguments in summary.lm allowed for this, if I understand it correctly.

That is, excluding factorial variables, the output would be only for x1:

> summary(ols, exclude=list(x2.f, x3.f)

Call:
lm(y ~ x1 + x2.f +x3.f)

Residuals:
   Min     1Q Median     3Q    Max 
-55.99 -38.66 -10.05  33.91 132.18 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept) 49.5283522  0.6035625  82.060  < 2e-16 ***
x1          -0.0002951  0.0000633  -4.663  3.2e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

回答1:


You can store the output of a summary and then just get the parts you want. If you just want part of the coefficient table - here is an example:

#make example data with 26 factors
X<-rnorm(26000)
Y<-rnorm(26000)
Z<-rep(letters,1000)

#do analysis, store summary and pull rows 1:5 of coefficient table
MyLM<-lm(X~Y+Z)
MySum<-summary(MyLM)
MySum$coef[1:5,]
#### or this produces the same output ###
coef(MySum)[1:5,]

This will give you the first five rows, you can use other indexing methods to get whatever you want, as the output of MySum$coef and coef(MySum) is a matrix.

For the example I stored the results of lm and of summary but you could combine this all in one line if you wanted, e.g. summary(lm(x...))$coef[1:5,]




回答2:


Precisely what you need can be done by saving part of output as a DataFrame. Then access only the part that is needed. This helps to show coefficients and also their p-values. First, you need to store the estimation results:

res<-lm(y ~ x1 + x2.f +x3.f)

Then you make data.frame out of the coefficients output (coefficients are 4th element of the full output):

res1<-data.frame(summary(res))[4])

With the dataframe you can now access/ limit to whatever you need. For example if only the first two coefficients and their p-values are needed:

res1[1:2,]


来源:https://stackoverflow.com/questions/29610821/r-hide-dummies-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!