require(stats); require(graphics)
plot(cars, xlab = "Speed (mph)", ylab = "Stopping distance (ft)",
     las = 1)

  1. Y=Time taken to stop, X1=stopping distance, X2=Speed, Y=X1/X2. Y is dependent variable, X1, X2 are independent variables. Y∼β0+β1X+ϵ

    Time_taken_to_stop <- 
    merge(x = cars$speed, 
          y = cars$dist)
# Fit a simple linear regression model 
linear_model <- lm(cars$speed ~ cars$dist, data = cars) 
# Print the summary of the linear model 
summary(linear_model)
## 
## Call:
## lm(formula = cars$speed ~ cars$dist, data = cars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.5293 -2.1550  0.3615  2.4377  6.4179 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.28391    0.87438   9.474 1.44e-12 ***
## cars$dist    0.16557    0.01749   9.464 1.49e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.156 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

The estimated intercept represents the predicted value of the response variable when the distancing to stop is zero. Cars$dist is the estimated coefficient for the predictor variable dist represents the change in the predicted value of the response variable for a one-unit change in the dist variable. And estimate is the estimated values of the coefficients. The t-statistic, which is the estimate divided by its standard error. It measures how many standard errors the estimate is from zero. The p-value associated with the t-statistic. It indicates whether the estimated coefficient is significantly different from zero. The Signif. codes section provides a shorthand representation of the p-values: *** means highly significant, ** means significant, * means marginally significant, and so on. The residual standard error (RSE) is an estimate of the standard deviation of the residuals. It gives idea of how much the observed values deviate from the predicted values on average, it’s 3.156.

library(writexl)
# Create a data frame with coefficients 
coefficients_df <- as.data.frame(coef(linear_model)) 
# Add other relevant information if needed 
summary_df <- data.frame( Residual_SE = summary(linear_model)$sigma, R_squared = summary(linear_model)$r.squared ) 
# Combine the data frames 
export_data <- cbind(coefficients_df, summary_df)
write_xlsx(export_data, "/Users/timyang/Downloads/exported_data.xlsx")
print(linear_model)
## 
## Call:
## lm(formula = cars$speed ~ cars$dist, data = cars)
## 
## Coefficients:
## (Intercept)    cars$dist  
##      8.2839       0.1656
print(coefficients_df)
##             coef(linear_model)
## (Intercept)          8.2839056
## cars$dist            0.1655676
print(summary_df)
##   Residual_SE R_squared
## 1    3.155753 0.6510794
print(export_data)
##             coef(linear_model) Residual_SE R_squared
## (Intercept)          8.2839056    3.155753 0.6510794
## cars$dist            0.1655676    3.155753 0.6510794
slopes <- coef(linear_model)[-1] 
print(slopes)
## cars$dist 
## 0.1655676