1&2
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
library(datasets)
data(mtcars)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
regression <- lm(mpg ~ disp + hp, data=mtcars)
stargazer(mtcars, type = "text")
##
## =============================================================
## Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
## -------------------------------------------------------------
## mpg 32 20.091 6.027 10 15.4 22.8 34
## cyl 32 6.188 1.786 4 4 8 8
## disp 32 230.722 123.939 71 120.8 326 472
## hp 32 146.688 68.563 52 96.5 180 335
## drat 32 3.597 0.535 2.760 3.080 3.920 4.930
## wt 32 3.217 0.978 1.513 2.581 3.610 5.424
## qsec 32 17.849 1.787 14.500 16.892 18.900 22.900
## vs 32 0.438 0.504 0 0 1 1
## am 32 0.406 0.499 0 0 1 1
## gear 32 3.688 0.738 3 3 4 5
## carb 32 2.812 1.615 1 2 4 8
## -------------------------------------------------------------
stargazer(regression, type = "text")
##
## ===============================================
## Dependent variable:
## ---------------------------
## mpg
## -----------------------------------------------
## disp -0.030***
## (0.007)
##
## hp -0.025*
## (0.013)
##
## Constant 30.736***
## (1.332)
##
## -----------------------------------------------
## Observations 32
## R2 0.748
## Adjusted R2 0.731
## Residual Std. Error 3.127 (df = 29)
## F Statistic 43.095*** (df = 2; 29)
## ===============================================
## Note: *p<0.1; **p<0.05; ***p<0.01
First talk about variables, disp has largest standard deviation and vs, am are binary. My dependent variable is mpg. And independent variables are hp and disp. Then talk about regression, R^2 is 0.748 meaning 74.8 % of Y is explained by X. Good fit. Finally talk about coefficients. hp is significant at * and disp is significant at ***. Both has negative relationship with Y and they are almost equally important. Sign is as expected just like real life.
3
par(mfrow=c(2,2))
plot(regression)
par(mfrow=c(1,1))
1) linearity: yes 2) Error independent: yes 3) Homoscedasticity: yes 4) Residual normal distributed: yes
4&5 Gauss-Markov gives conditions for OLS to become BLUE. BLUE mean best linear unbiased estimator. I can summarize the conditions in my own words. 1) linearity: Y and X should be linear relationship 2)No Perfect Multicollinearity: We cannot have perfect Multicollinearity. 3) Homoscedasticity: error has constant variance. 4)(does not need this to be BLUE) Normality of errors: errors need to follow normal distribution 5) error uncorrelated to each other 6) X should be uncorrelated with error 7)The population mean of error should be zero.