library(cli)
## Warning: package 'cli' was built under R version 4.2.3
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.2.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ purrr 1.0.2
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.5.1 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
# Load the Data
df <- read.csv("D:/college life/Semester 6/Coursera/Course 10/day.csv")
# Summary of the Data
summary(df[, c("dteday", "temp", "cnt")])
## dteday temp cnt
## Length:731 Min. :0.05913 Min. : 22
## Class :character 1st Qu.:0.33708 1st Qu.:3152
## Mode :character Median :0.49833 Median :4548
## Mean :0.49538 Mean :4504
## 3rd Qu.:0.65542 3rd Qu.:5956
## Max. :0.86167 Max. :8714
# Convert dteday to Date Format
df$dteday <- ymd(df$dteday)
# Extract Month Names
df$month_name <- month(df$dteday, label = TRUE, abbr = FALSE)
# Convert to Character
df$month_name <- as.character(df$month_name)
# Simple linear regression model
Model1 <- lm(cnt ~ month_name, data = df)
# Summary of Model 1
summary(Model1)
##
## Call:
## lm(formula = cnt ~ month_name, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5177.2 -1095.2 -249.3 1290.0 4669.7
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4484.9 196.7 22.799 < 2e-16 ***
## month_nameAugust 1179.5 275.9 4.275 2.17e-05 ***
## month_nameDecember -1081.1 275.9 -3.918 9.79e-05 ***
## month_nameFebruary -1829.6 281.8 -6.492 1.58e-10 ***
## month_nameJanuary -2308.6 275.9 -8.366 3.09e-16 ***
## month_nameJuly 1078.8 275.9 3.909 0.000101 ***
## month_nameJune 1287.5 278.2 4.628 4.38e-06 ***
## month_nameMarch -792.6 275.9 -2.873 0.004192 **
## month_nameMay 864.9 275.9 3.134 0.001793 **
## month_nameNovember -237.7 278.2 -0.854 0.393113
## month_nameOctober 714.3 275.9 2.589 0.009829 **
## month_nameSeptember 1281.6 278.2 4.607 4.83e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1524 on 719 degrees of freedom
## Multiple R-squared: 0.3906, Adjusted R-squared: 0.3813
## F-statistic: 41.9 on 11 and 719 DF, p-value: < 2.2e-16
Based on the results of the regression analysis, the linear regression model equation can be expressed as follows: cnt = 4489.9 + 1179.5August - 1081.1December - 1829.6February - 2308.6January + 1078.8July + 1287.5June - 792.6March + 864.9May - 237.7November + 714.3October + 1281.6September
Interpretation: The Multiple R-squared value of 0.3906 indicates that around 39.06% of the variation in the number of bicycle rentals (cnt) can be explained by the month variable. Meanwhile, the Adjusted R-squared of 0.3813 indicates that after taking into account the number of predictor variables, this model is still able to explain around 38.13% of the variation in the data.
Reference month : April
Predicted cnt for the month of April: 4484.9
# Predicted cnt for January and June
january_cnt <- coef(Model1)["(Intercept)"] + coef(Model1)["month_nameJanuary"]
june_cnt <- coef(Model1)["(Intercept)"] + coef(Model1)["month_nameJune"]
january_cnt
## (Intercept)
## 2176.339
june_cnt
## (Intercept)
## 5772.367
# Multiple linear regression model
Model2 <- lm(cnt ~ temp + month_name, data = df)
# Summary of Model 2
summary(Model2)
##
## Call:
## lm(formula = cnt ~ temp + month_name, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4896.6 -1080.0 -228.4 1245.2 3372.9
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1554.39 390.76 3.978 7.66e-05 ***
## temp 6235.14 729.40 8.548 < 2e-16 ***
## month_nameAugust -308.08 315.42 -0.977 0.3290
## month_nameDecember -170.96 283.80 -0.602 0.5471
## month_nameFebruary -764.81 296.15 -2.582 0.0100 *
## month_nameJanuary -852.31 313.41 -2.719 0.0067 **
## month_nameJuly -701.18 335.50 -2.090 0.0370 *
## month_nameJune -47.47 307.78 -0.154 0.8775
## month_nameMarch -297.20 269.38 -1.103 0.2703
## month_nameMay 86.73 278.37 0.312 0.7555
## month_nameNovember 390.66 275.22 1.419 0.1562
## month_nameOctober 620.72 263.30 2.357 0.0187 *
## month_nameSeptember 368.25 285.93 1.288 0.1982
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1453 on 718 degrees of freedom
## Multiple R-squared: 0.4469, Adjusted R-squared: 0.4377
## F-statistic: 48.35 on 12 and 718 DF, p-value: < 2.2e-16
Based on the regression output, the multiple regression model equation can be written as follows: cnt = 1554.9 + 6235.14temp - 308.08August - 170.96December - 764.81February - 852.31January - 701.18July - 47.47June - 297.20March + 86.73May + 390.66November + 620.72October + 368.25September
Interpretation: The Multiple R-squared value of 0.4469 indicates that around 44.69% of the variation in the number of bicycle rentals (cnt) can be explained by the month and temperature variables. Meanwhile, the Adjusted R-squared of 0.4377 indicates that after adjusting for the number of predictor variables, this model is still able to explain around 43.77% of the variation in the data.
Comparison of R-squared Values: The Adjusted R-squared value in Model 1 is 38.13% lower than the Adjusted R-squared in Model 2 which is 43.77%. This is because in Model 2 the temperature variable (temp) is added which turns out to have a significant effect on the number of bicycle rentals (cnt).
coef_model1_januari <- coef(Model1)["month_nameJanuary"]
coef_model2_januari <- coef(Model2)["month_nameJanuary"]
cat("Coefficient of January in Model1:", coef_model1_januari, "\n")
## Coefficient of January in Model1: -2308.561
cat("Coefficient of January in Model2:", coef_model2_januari, "\n")
## Coefficient of January in Model2: -852.3127
Interpretation: The coefficient for January in Model 2 is smaller (closer to zero) compared to Model 1 due to the addition of the temperature variable (temp). In Model 1, the effect of January reflects the full impact of winter, including the low temperatures that cause a decrease in bike rentals. However, when the temperature variable is included in Model 2, most of the winter impact is explained by temperature directly. As a result, the specific effect of January on bike rentals becomes smaller. This is the reason why the coefficient value for month_nameJanuary changes significantly between the two models.
# Predicted cnt for January when temp = 0.25
predicted_cnt_jan <- coef(Model2)["(Intercept)"] + coef(Model2)["month_nameJanuary"] + (0.25 * coef(Model2)["temp"])
predicted_cnt_jan
## (Intercept)
## 2260.863