Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Hide control variables from lm display

Tags:

r

lm

I would like to do a linear regression with y being my dependent variable and x1 x2 x3 being my independent variables. I also have "control" variables z1 z2 z3 which I would like to include but not display the results of.

summary(lm(y~x1+x2+x3+z1+z2+z3))

Is there a way for summary not to display the coefficients for my control variables?

like image 491
Francis Smart Avatar asked Mar 16 '23 15:03

Francis Smart


1 Answers

As @MrFlick suggested, using summary.lm can help.

Code:

# Build Sample Data
df <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100), z1 = rnorm(100), z2 = rnorm(100), z3 = rnorm(100))

# Run Model
sum <- summary.lm(lm(y ~ x1 + x2 + x3 + z1 + z2 + z3, data = df))

# Remove z1:z3
sum$coefficients <- sum$coefficients[1:4,]

# Print Results
print(sum)

Output :

Call:
lm(formula = y ~ x1 + x2 + x3 + z1 + z2 + z3, data = df)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.76472 -0.56958 -0.02673  0.50188  2.61362 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)  0.18092    0.09966   1.815   0.0727 .
x1           0.12282    0.10231   1.201   0.2330  
x2          -0.22411    0.10781  -2.079   0.0404 *
x3          -0.01096    0.09554  -0.115   0.9090  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.9596 on 93 degrees of freedom
Multiple R-squared:  0.07717,   Adjusted R-squared:  0.01763 
F-statistic: 1.296 on 6 and 93 DF,  p-value: 0.2667
like image 171
Vedda Avatar answered Mar 28 '23 06:03

Vedda