library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
data <- cars
stargazer(data = data, type = "text", title = "Table 1: Data Summary Statistics")
##
## Table 1: Data Summary Statistics
## ====================================
## Statistic N Mean St. Dev. Min Max
## ------------------------------------
## speed 50 15.400 5.288 4 25
## dist 50 42.980 25.769 2 120
## ------------------------------------
The dependent variable is dist (Y) and the independent variable is speed (x). The estimate equation is: \[Y \sim \beta_0 + \beta_1X + \epsilon\]
lm_model <- lm(dist ~ speed, data = data)
summary(lm_model)
##
## Call:
## lm(formula = dist ~ speed, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.069 -9.525 -2.272 9.215 43.201
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -17.5791 6.7584 -2.601 0.0123 *
## speed 3.9324 0.4155 9.464 1.49e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.38 on 48 degrees of freedom
## Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
## F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
Slope represents the change in the dependent
variable for a one-unit increase in the independent variable. It
indicates how much the stopping distance (‘dist’) is expected to change
for each one-unit increase in speed (‘speed’).
Intercept is the value of the dependent variable when
the independent variable is zero. In the ‘cars’ dataset, the intercept
represents the estimated stopping distance when the speed of the car is
zero.
co <- cov(cars$speed, cars$dist)
vars <- var(cars$speed)
slope <- co / vars
mean_speed <- mean(cars$speed)
mean_dist <- mean(cars$dist)
intercept <- mean_dist - slope * mean_speed
cat("Slope:", slope, "\n")
## Slope: 3.932409
cat("Intercept:", intercept, "\n")
## Intercept: -17.57909