data<-datasets::cars
head(data)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
You can continue to use the dataset from your last discussion, or pick up a new dataset.
#Dependent variable is "dist", measurements (in feet) of stopping distance.
#Independent variable is "speed", measuring the speed (in mph) of the car.
Type put your estimating equation.i.e. I am expecting to see subscripts i on your y, x and error term professionally done.
\[ y_i = \beta_0 + X_i\beta_1 + \epsilon_i \] \[y_i\] is the measurement (in feet) of the distance observation i. \[x_i\] is the speed of observation i. \[\beta_0\] is the y-intercept parameter \[\beta_1\] is the slope parameter \[\epsilon_i\] is the random error term for observation i.
Make sure to describe these two variables (y measures number of cars, x is income in 1000s of dollars).
model<-lm(dist~speed,data = data)
summary(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
model$coefficients
## (Intercept) speed
## -17.579095 3.932409
#Intercept is -17.58. The interpretation is: if a car's speed is 0 mph, the predicted stopping distance is -17.58 feet.
#Slope is 3.93. The interpretation is: for every 1 mph increase in a car's speed, the stopping distance increases by 3.93 feet.
Slope: \[\hat{\beta_1} = \frac{Cov(X,Y)}{Var(X)}\] Intercept: \[\hat{\beta_0}=\bar{Y}-\hat{\beta_1}\bar{X}\]
#Calculate means
x_bar<-mean(data$speed)
y_bar<-mean(data$dist)
#Calculate covariance and variance
cov_xy<-cov(data$speed,data$dist)
var_x<-var(data$speed)
#Calculate slope
beta_1<-cov_xy/var_x
#Calculate intercept
beta_0<-y_bar-beta_1*x_bar
beta_1
## [1] 3.932409
beta_0
## [1] -17.57909
#We get the same slope and intercept using the variance/covariance formulas as we do using the R lm() formula.