0

What is the best way to generate good looking regression tables for slidify?

---
## Custom Tables

```{r, results = "asis", echo = FALSE}
library(xtable)
OLS <- lm(hp ~ wt, mtcars)
print(xtable(OLS), type="html", html.table.attributes='class=mytable', label ="OLS", digits = 3)
```

<style>
table.mytable {
  border: none;
  width: 100%;
  border-collapse: collapse;
  font-size: 45px;
  line-height: 50px;
  font-family: 'Ubuntu';'Trebuchet MS';
  font-weight: bolder;
  color: blue;
}

table.mytable tr:nth-child(2n+1) {
/*  background: #E8F2FF; */
  background: #FFFFFF;
}
</style>

I would like to be able to change the names ('Constant' instead of Intercept, and 'Weight' instead of wt), add the number of observations, R-squared, F Statistic, etc.

Thanks!

Ignacio
  • 7,646
  • 16
  • 60
  • 113

1 Answers1

0

First,

# Check what's inside your OLS object:
names(OLS)
 [1] "coefficients" 
 [2] "residuals"    
 [3] "effects"      
 [4] "rank"         
 [5] "fitted.values"
 [6] "assign"       
 [7] "qr"           
 [8] "df.residual"  
 [9] "xlevels"      
[10] "call"         
[11] "terms"        
[12] "model"        

# Look inside coeff:
names(OLS$coeff)
[1] "(Intercept)"
[2] "wt"         

# Rename:
names(OLS$coeff) <- c("Constant", "Weight")

# Check the new names:
names(OLS$coeff)
[1] "Constant" "Weight"

Secondly, The R-squared can be found in a similar way

summary(OLS)

Call:
lm(formula = hp ~ wt, data = mtcars)

Residuals:
    Min      1Q  Median 
-83.430 -33.596 -13.587 
     3Q     Max 
  7.913 172.030 

Coefficients:
            Estimate
(Intercept)   -1.821
wt            46.160
            Std. Error
(Intercept)     32.325
wt               9.625
            t value Pr(>|t|)
(Intercept)  -0.056    0.955
wt            4.796 4.15e-05

(Intercept)    
wt          ***
---
Signif. codes:  
  0 ‘***’ 0.001 ‘**’ 0.01
  ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 52.44 on 30 degrees of freedom
Multiple R-squared:  0.4339,    Adjusted R-squared:  0.4151 
F-statistic:    23 on 1 and 30 DF,  p-value: 4.146e-05

You can str(summary(OLS)) to see more information. Thus:

 summary(OLS)$r.squared
[1] 0.4339488
PatrickT
  • 10,037
  • 9
  • 76
  • 111