Linear Regression is a statistical method for modeling the relationship between a dependent variable and one or more independent variables. I’m using the ‘msleep’ dataset for my presentation.
2024-06-04
1
Linear Regression is a statistical method for modeling the relationship between a dependent variable and one or more independent variables. I’m using the ‘msleep’ dataset for my presentation.
\[ \hat{Y} = b_0 + b_1X \]
\(\hat{Y}\) is the predicted value of the dependent variable
\(X\) is the independent variable
\(b_0\) is the intercept
\(b_1\) is the slope
Let’s consider the msleep dataset which includes various data on the sleep habits of mammals.
data(msleep) head(msleep)
## # A tibble: 6 × 11 ## name genus vore order conservation sleep_total sleep_rem sleep_cycle awake ## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> ## 1 Cheetah Acin… carni Carn… lc 12.1 NA NA 11.9 ## 2 Owl mo… Aotus omni Prim… <NA> 17 1.8 NA 7 ## 3 Mounta… Aplo… herbi Rode… nt 14.4 2.4 NA 9.6 ## 4 Greate… Blar… omni Sori… lc 14.9 2.3 0.133 9.1 ## 5 Cow Bos herbi Arti… domesticated 4 0.7 0.667 20 ## 6 Three-… Brad… herbi Pilo… <NA> 14.4 2.2 0.767 9.6 ## # ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>
A plot of the linear model
data(msleep)
msleep_data <- msleep[, c("bodywt", "sleep_total")]
msleep_data = na.omit(msleep_data)
model <- lm(formula = msleep$sleep_total ~ msleep$bodywt, data = msleep_data)
Diet = msleep$vore
plot1 = ggplot(msleep_data, aes(x = bodywt, y = sleep_total, color = Diet))
+ geom_point() + geom_smooth(method = "lm", col = "darkred")+
ggtitle("Linear Regression of Mammals' Sleep") +
xlab("Body Weight") + ylab("Total Sleep in hours")
+ xlim(0, 1000)
print(plot1)
A residual plot of the linear model
Plot using plot_ly
## ## Call: ## lm(formula = msleep$sleep_total ~ msleep$bodywt, data = msleep_data) ## ## Residuals: ## Min 1Q Median 3Q Max ## -7.7008 -2.3787 -0.4268 3.2732 9.1731 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 10.7269205 0.4773797 22.470 < 2e-16 *** ## msleep$bodywt -0.0017647 0.0005971 -2.956 0.00409 ** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 4.254 on 81 degrees of freedom ## Multiple R-squared: 0.09735, Adjusted R-squared: 0.08621 ## F-statistic: 8.736 on 1 and 81 DF, p-value: 0.004085