day <- read.csv("~/Documents/UW/2023 Fall/SOC 325/Final project/bike+sharing+dataset/day.csv")
size_sum(day)
## [1] "[731 × 16]"
coul <- colorRampPalette(brewer.pal(8, "PiYG"))(25)

data_map <- day %>%
  select(
    season, yr, mnth, holiday, weekday, workingday, weathersit, 
    temp, atemp, hum, windspeed, casual, registered, cnt
    ) %>%
  cor() %>%
  as.matrix() %>%
  corrplot::corrplot(
    method = "color",
    col = coul,
    main = "Correlation Heatmap",
    addCoef.col = "black",
    number.cex = 0.4,
    tl.cex = 0.7,
    tl.srt = 45,
    tl.col = "black",
    mar = c(1, 1, 3, 1)
  )

data_mat <- day %>%
  select(
    season, yr, mnth, holiday, weekday, workingday, weathersit, 
    temp, atemp, hum, windspeed, casual, registered, cnt
    ) %>%
  cor() %>%
  as.matrix()

print(data_mat)
##                  season           yr         mnth      holiday       weekday
## season      1.000000000 -0.001844343  0.831440114 -0.010536659 -0.0030798813
## yr         -0.001844343  1.000000000 -0.001792434  0.007954311 -0.0054607652
## mnth        0.831440114 -0.001792434  1.000000000  0.019190895  0.0095093129
## holiday    -0.010536659  0.007954311  0.019190895  1.000000000 -0.1019602689
## weekday    -0.003079881 -0.005460765  0.009509313 -0.101960269  1.0000000000
## workingday  0.012484963 -0.002012621 -0.005900951 -0.253022700  0.0357896736
## weathersit  0.019211028 -0.048726541  0.043528098 -0.034626841  0.0310874694
## temp        0.334314856  0.047603572  0.220205335 -0.028555535 -0.0001699624
## atemp       0.342875613  0.046106149  0.227458630 -0.032506692 -0.0075371318
## hum         0.205444765 -0.110651045  0.222203691 -0.015937479 -0.0522321004
## windspeed  -0.229046337 -0.011817060 -0.207501752  0.006291507  0.0142821241
## casual      0.210399165  0.248545664  0.123005889  0.054274203  0.0599226375
## registered  0.411623051  0.594248168  0.293487830 -0.108744863  0.0573674440
## cnt         0.406100371  0.566709708  0.279977112 -0.068347716  0.0674434124
##              workingday  weathersit          temp        atemp         hum
## season      0.012484963  0.01921103  0.3343148564  0.342875613  0.20544476
## yr         -0.002012621 -0.04872654  0.0476035719  0.046106149 -0.11065104
## mnth       -0.005900951  0.04352810  0.2202053352  0.227458630  0.22220369
## holiday    -0.253022700 -0.03462684 -0.0285555350 -0.032506692 -0.01593748
## weekday     0.035789674  0.03108747 -0.0001699624 -0.007537132 -0.05223210
## workingday  1.000000000  0.06120043  0.0526598102  0.052182275  0.02432705
## weathersit  0.061200430  1.00000000 -0.1206022365 -0.121583354  0.59104460
## temp        0.052659810 -0.12060224  1.0000000000  0.991701553  0.12696294
## atemp       0.052182275 -0.12158335  0.9917015532  1.000000000  0.13998806
## hum         0.024327046  0.59104460  0.1269629390  0.139988060  1.00000000
## windspeed  -0.018796487  0.03951106 -0.1579441204 -0.183642967 -0.24848910
## casual     -0.518044191 -0.24735300  0.5432846617  0.543863690 -0.07700788
## registered  0.303907117 -0.26038771  0.5400119662  0.544191758 -0.09108860
## cnt         0.061156063 -0.29739124  0.6274940090  0.631065700 -0.10065856
##               windspeed      casual  registered         cnt
## season     -0.229046337  0.21039916  0.41162305  0.40610037
## yr         -0.011817060  0.24854566  0.59424817  0.56670971
## mnth       -0.207501752  0.12300589  0.29348783  0.27997711
## holiday     0.006291507  0.05427420 -0.10874486 -0.06834772
## weekday     0.014282124  0.05992264  0.05736744  0.06744341
## workingday -0.018796487 -0.51804419  0.30390712  0.06115606
## weathersit  0.039511059 -0.24735300 -0.26038771 -0.29739124
## temp       -0.157944120  0.54328466  0.54001197  0.62749401
## atemp      -0.183642967  0.54386369  0.54419176  0.63106570
## hum        -0.248489099 -0.07700788 -0.09108860 -0.10065856
## windspeed   1.000000000 -0.16761335 -0.21744898 -0.23454500
## casual     -0.167613349  1.00000000  0.39528245  0.67280443
## registered -0.217448981  0.39528245  1.00000000  0.94551692
## cnt        -0.234544997  0.67280443  0.94551692  1.00000000
day_data <- day %>%
  mutate(
    mnth = case_when(
      mnth == 1 ~ "Jan",
      mnth == 2 ~ "Feb",
      mnth == 3 ~ "Mar",
      mnth == 4 ~ "Apr",
      mnth == 5 ~ "May",
      mnth == 6 ~ "Jun",
      mnth == 7 ~ "Jul",
      mnth == 8 ~ "Aug",
      mnth == 9 ~ "Sep",
      mnth == 10 ~ "Oct",
      mnth == 11 ~ "Nov",
      mnth == 12 ~ "Dec",
      TRUE ~ as.character(mnth)
    ),
    season = case_when(
      season == 1 ~ "Spring",
      season == 2 ~ "Summer",
      season == 3 ~ "Autumn",
      season == 4 ~ "Winter",
      TRUE ~ as.character(season)
    ),
    weathersit = case_when(
      weathersit == 1 ~ "Clear",
      weathersit == 2 ~ "Mist",
      weathersit == 3 ~ "Light",
      weathersit == 4 ~ "Bad",
      TRUE ~ as.character(weathersit)
    ),
    workday_combined = case_when(
      workingday == 1 ~ "Workday",
      workingday == 0 ~ "Holiday",
      TRUE ~ as.character(workingday)
    )
  )

head(day_data)
##   instant     dteday season yr mnth holiday weekday workingday weathersit
## 1       1 2011-01-01 Spring  0  Jan       0       6          0       Mist
## 2       2 2011-01-02 Spring  0  Jan       0       0          0       Mist
## 3       3 2011-01-03 Spring  0  Jan       0       1          1      Clear
## 4       4 2011-01-04 Spring  0  Jan       0       2          1      Clear
## 5       5 2011-01-05 Spring  0  Jan       0       3          1      Clear
## 6       6 2011-01-06 Spring  0  Jan       0       4          1      Clear
##       temp    atemp      hum windspeed casual registered  cnt workday_combined
## 1 0.344167 0.363625 0.805833 0.1604460    331        654  985          Holiday
## 2 0.363478 0.353739 0.696087 0.2485390    131        670  801          Holiday
## 3 0.196364 0.189405 0.437273 0.2483090    120       1229 1349          Workday
## 4 0.200000 0.212122 0.590435 0.1602960    108       1454 1562          Workday
## 5 0.226957 0.229270 0.436957 0.1869000     82       1518 1600          Workday
## 6 0.204348 0.233209 0.518261 0.0895652     88       1518 1606          Workday
season_colors <- c("Spring" = "#B51E3BFF", "Summer" = "#ED564DFF", "Autumn" = "#F78273FF", "Winter" = "#FEAA9AFF")

day_data %>%
  ggplot(aes(y = as.numeric(cnt), x = dteday, color = as.factor(season))) +
  geom_point() +
  scale_color_manual(values = season_colors) +
  theme_bw() +
  ylab("Total count of rental bikes each day") +
  xlab("Date") +
  ggtitle("Scatter Plot for Total Rental Counts")

data_total <- day_data %>%
  group_by(season) %>%
  summarise(
    Avr_rent = mean(cnt)
  )
data_total
## # A tibble: 4 × 2
##   season Avr_rent
##   <chr>     <dbl>
## 1 Autumn    5644.
## 2 Spring    2604.
## 3 Summer    4992.
## 4 Winter    4728.
data2011 <- day_data %>%
  filter(yr == "0") %>%
  group_by(season) %>%
    summarise(
    Avr_rent_2011 = mean(cnt)
  )
data2011
## # A tibble: 4 × 2
##   season Avr_rent_2011
##   <chr>          <dbl>
## 1 Autumn         4464.
## 2 Spring         1667.
## 3 Summer         3775.
## 4 Winter         3664.
data2012 <- day_data %>%
  filter(yr == "1") %>%
  group_by(season) %>%
    summarise(
    Avr_rent_2012 = mean(cnt)
  )
data2012
## # A tibble: 4 × 2
##   season Avr_rent_2012
##   <chr>          <dbl>
## 1 Autumn         6824.
## 2 Spring         3531.
## 3 Summer         6209.
## 4 Winter         5792.
combined_data <- bind_cols(
  data_total %>% select(season, Avr_rent),
  data2011 %>% select(Avr_rent_2011),
  data2012 %>% select(Avr_rent_2012)
)
colnames(combined_data) <- c("season", "Avr_rent_total", "Avr_rent_2011", "Avr_rent_2012")
print(combined_data)
## # A tibble: 4 × 4
##   season Avr_rent_total Avr_rent_2011 Avr_rent_2012
##   <chr>           <dbl>         <dbl>         <dbl>
## 1 Autumn          5644.         4464.         6824.
## 2 Spring          2604.         1667.         3531.
## 3 Summer          4992.         3775.         6209.
## 4 Winter          4728.         3664.         5792.
max(data_total$Avr_rent)
## [1] 5644.303
day %>%
    filter(season == "3") %>%
  select(yr, mnth, holiday, weekday, workingday, weathersit, 
    temp, atemp, hum, windspeed, casual, registered, cnt
    ) %>%
  cor() %>%
  as.matrix() %>%
  corrplot::corrplot(
    method = "color",
    col = coul,
    main = "Correlation Heatmap - Autumn",
    addCoef.col = "black",
    number.cex = 0.4,
    tl.cex = 0.7,
    tl.srt = 45,
    tl.col = "black",
    mar = c(1, 1, 3, 1)
  )

# Plot for data_total
hist_total <- ggplot(data_total, aes(x = season, y = Avr_rent, fill = season)) +
  geom_bar(stat = "identity") +
  labs(title = "Average Rent by Season - Total Data",
       x = "Season",
       y = "Average Rent")
hist_total

# Plot for data2011
hist_2011 <- ggplot(data2011, aes(x = season, y = Avr_rent_2011, fill = season)) +
  geom_bar(stat = "identity") +
  labs(title = "Average Rent by Season - Data 2011",
       x = "Season",
       y = "Average Rent")
hist_2011

# Plot for data2012
hist_2012 <- ggplot(data2012, aes(x = season, y = Avr_rent_2012, fill = season)) +
  geom_bar(stat = "identity") +
  labs(title = "Average Rent by Season - Data 2012",
       x = "Season",
       y = "Average Rent")
hist_2012

day_data %>% 
  ggplot(aes(x = registered, y = cnt))+
  geom_point(color = "red") + 
  geom_smooth(method = "lm") +
  xlab("Registered users") +
  ylab("Rental Counts") +
  ggtitle("Linear regression: registered")
## `geom_smooth()` using formula 'y ~ x'

day_data %>% 
  ggplot(aes(x = casual, y = cnt))+
  geom_point(color = "red") + 
  geom_smooth(method = "lm") +
  xlab("Registered users") +
  ylab("Rental Counts") +
  ggtitle("Linear regression: casual")
## `geom_smooth()` using formula 'y ~ x'

#lm registered
summary(lm(registered ~ cnt, data = day_data))
## 
## Call:
## lm(formula = registered ~ cnt, data = day_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1937.22  -167.95    95.07   348.41   851.71 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.260e+02  4.761e+01   4.746  2.5e-06 ***
## cnt         7.615e-01  9.712e-03  78.412  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 508.3 on 729 degrees of freedom
## Multiple R-squared:  0.894,  Adjusted R-squared:  0.8939 
## F-statistic:  6149 on 1 and 729 DF,  p-value: < 2.2e-16
#lm casual
summary(lm(casual ~ cnt, data = day_data))
## 
## Call:
## lm(formula = casual ~ cnt, data = day_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -851.71 -348.41  -95.07  167.95 1937.22 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2.260e+02  4.761e+01  -4.746  2.5e-06 ***
## cnt          2.385e-01  9.712e-03  24.554  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 508.3 on 729 degrees of freedom
## Multiple R-squared:  0.4527, Adjusted R-squared:  0.4519 
## F-statistic: 602.9 on 1 and 729 DF,  p-value: < 2.2e-16
casual_colors <- c("Spring" = "#2E5A87FF", "Summer" = "#5888B3FF", "Autumn" = "#719FC6FF", "Winter" = "#ADCFE6FF")
day_data %>%
  ggplot(aes(y = as.numeric(casual), x = dteday, color = as.factor(season))) +
  geom_point() +
  scale_color_manual(values = casual_colors) +
  theme_bw() +
  ylab("Rental bikes by casual users") +
  xlab("Date") +
  ggtitle("Scatter Plot for Rental by Casual Users")

registered_colors <- c("Spring" = "#2C8A3FFF", "Summer" = "#559E55FF", "Autumn" = "#82B275FF", "Winter" = "#A8C297FF")
day_data %>%
  ggplot(aes(y = as.numeric(registered), x = dteday, color = as.factor(season))) +
  geom_point() +
  scale_color_manual(values = registered_colors) +
  theme_bw() +
  ylab("Rental bikes by casual users") +
  xlab("Date") +
  ggtitle("Scatter Plot for Rental by Registered Users")

casual_colors1 <- c("Casual" = "#5888B3FF")
registered_colors1 <- c("Registered" =  "#559E55FF")

day_data %>%
  ggplot(aes(x = dteday)) +
  geom_point(aes(y = as.numeric(casual), color = casual_colors1), size = 1.5) +
  geom_point(aes(y = as.numeric(registered), color = registered_colors1), size = 1.5) +
  theme_bw() +
  ylab("Rental bikes") +
  xlab("Date") +
  ggtitle("Scatter Plot for Rental by Registered and Casual Users")

day_data %>%
  ggplot( aes(x = dteday, y = cnt, group = workday_combined, color = workday_combined)) +
  ylab("Rental Counts") + 
  xlab("Date") +
  ggtitle("Rental Counts on Holiday and Workday") +
  geom_line()

day_data %>%
  ggplot(aes(x = dteday, y = cnt, group = weathersit, color = weathersit)) +
  ylab("Rental Counts") + 
  xlab("Date") +
  ggtitle("Rental Counts by Weather Situation") +
  geom_line()