R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── 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(readr)

# Read the CSV file
bike_data <- read_csv("C:/Users/os/Desktop/bikes.csv")
## Rows: 731 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (9): season, holiday, weekday, weather, temperature, realfeel, humidity...
## date (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(bike_data)
## # A tibble: 6 × 10
##   date       season holiday weekday weather temperature realfeel humidity
##   <date>      <dbl>   <dbl>   <dbl>   <dbl>       <dbl>    <dbl>    <dbl>
## 1 2011-01-01      1       0       6       2        46.7     46.4    0.806
## 2 2011-01-02      1       0       0       2        48.4     45.2    0.696
## 3 2011-01-03      1       0       1       1        34.2     25.7    0.437
## 4 2011-01-04      1       0       2       1        34.5     28.4    0.590
## 5 2011-01-05      1       0       3       1        36.8     30.4    0.437
## 6 2011-01-06      1       0       4       1        34.9     30.9    0.518
## # ℹ 2 more variables: windspeed <dbl>, rentals <dbl>

1:is there is a relationship between the no of bikee rentals and any of other variables?

Perform multilinearRegression

model <- lm(rentals ~ season + holiday + weekday + weather + temperature + realfeel + humidity + windspeed, data = bike_data)

# Print summary of the regression model
summary(model)
## 
## Call:
## lm(formula = rentals ~ season + holiday + weekday + weather + 
##     temperature + realfeel + humidity + windspeed, data = bike_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4355.7  -969.0  -219.4  1056.3  3916.6 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2512.37     451.00   5.571 3.58e-08 ***
## season        405.26      48.79   8.307 4.85e-16 ***
## holiday      -574.56     298.92  -1.922   0.0550 .  
## weekday        60.54      25.03   2.419   0.0158 *  
## weather      -481.99     119.94  -4.019 6.47e-05 ***
## temperature    28.40      25.49   1.114   0.2656    
## realfeel       30.94      20.55   1.505   0.1326    
## humidity    -2301.19     476.26  -4.832 1.65e-06 ***
## windspeed     -75.62      16.82  -4.497 8.04e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1340 on 722 degrees of freedom
## Multiple R-squared:  0.5264, Adjusted R-squared:  0.5212 
## F-statistic: 100.3 on 8 and 722 DF,  p-value: < 2.2e-16

Q2:is there a relationship how strong it ?

correlations <- cor(bike_data[c("rentals", "season", "holiday", "weekday", "weather", "temperature", "realfeel", "humidity", "windspeed")])

print(correlations)
##                 rentals       season      holiday       weekday     weather
## rentals      1.00000000  0.406100371 -0.068347716  0.0674434124 -0.29739124
## season       0.40610037  1.000000000 -0.010536659 -0.0030798813  0.01921103
## holiday     -0.06834772 -0.010536659  1.000000000 -0.1019602689 -0.03462684
## weekday      0.06744341 -0.003079881 -0.101960269  1.0000000000  0.03108747
## weather     -0.29739124  0.019211028 -0.034626841  0.0310874694  1.00000000
## temperature  0.62749401  0.334314856 -0.028555535 -0.0001699624 -0.12060224
## realfeel     0.63106570  0.342875613 -0.032506692 -0.0075371318 -0.12158335
## humidity    -0.10065856  0.205444765 -0.015937479 -0.0522321004  0.59104460
## windspeed   -0.23454500 -0.229046337  0.006291507  0.0142821241  0.03951106
##               temperature     realfeel    humidity    windspeed
## rentals      0.6274940090  0.631065700 -0.10065856 -0.234544997
## season       0.3343148564  0.342875613  0.20544476 -0.229046337
## holiday     -0.0285555350 -0.032506692 -0.01593748  0.006291507
## weekday     -0.0001699624 -0.007537132 -0.05223210  0.014282124
## weather     -0.1206022365 -0.121583354  0.59104460  0.039511059
## temperature  1.0000000000  0.991701553  0.12696294 -0.157944120
## realfeel     0.9917015532  1.000000000  0.13998806 -0.183642967
## humidity     0.1269629390  0.139988060  1.00000000 -0.248489099
## windspeed   -0.1579441204 -0.183642967 -0.24848910  1.000000000

1 indicates a perfect positive linear relationship, -1 indicates a perfect negative linear relationship, and 0 indicates no linear relationship.

Q3:IS THE RELATION IS LINEAR?

# Scatter plot between rentals and season
plot(bike_data$season, bike_data$rentals, xlab = "Season", ylab = "Rentals", main = "Rentals vs. Season")

# Scatter plot between rentals and holiday
plot(bike_data$holiday, bike_data$rentals, xlab = "Holiday", ylab = "Rentals", main = "Rentals vs. Holiday")

# Scatter plot between rentals and weekday
plot(bike_data$weekday, bike_data$rentals, xlab = "Weekday", ylab = "Rentals", main = "Rentals vs. Weekday")

# Scatter plot between rentals and weather
plot(bike_data$weather, bike_data$rentals, xlab = "Weather", ylab = "Rentals", main = "Rentals vs. Weather")

# Scatter plot between rentals and temperature
plot(bike_data$temperature, bike_data$rentals, xlab = "Temperature", ylab = "Rentals", main = "Rentals vs. Temperature")

# Scatter plot between rentals and realfeel
plot(bike_data$realfeel, bike_data$rentals, xlab = "Real Feel", ylab = "Rentals", main = "Rentals vs. Real Feel")

# Scatter plot between rentals and humidity
plot(bike_data$humidity, bike_data$rentals, xlab = "Humidity", ylab = "Rentals", main = "Rentals vs. Humidity")

# Scatter plot between rentals and windspeed
plot(bike_data$windspeed, bike_data$rentals, xlab = "Windspeed", ylab = "Rentals", main = "Rentals vs. Windspeed")

Q4:if relationship is linear how will you quantify the effect of variable on the no of bike rentals?

# Perform multilinear regression
model <- lm(rentals ~ season + holiday + weekday + weather + temperature + realfeel + humidity + windspeed, data = bike_data)

# Print summary of the regression model
summary(model)
## 
## Call:
## lm(formula = rentals ~ season + holiday + weekday + weather + 
##     temperature + realfeel + humidity + windspeed, data = bike_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4355.7  -969.0  -219.4  1056.3  3916.6 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2512.37     451.00   5.571 3.58e-08 ***
## season        405.26      48.79   8.307 4.85e-16 ***
## holiday      -574.56     298.92  -1.922   0.0550 .  
## weekday        60.54      25.03   2.419   0.0158 *  
## weather      -481.99     119.94  -4.019 6.47e-05 ***
## temperature    28.40      25.49   1.114   0.2656    
## realfeel       30.94      20.55   1.505   0.1326    
## humidity    -2301.19     476.26  -4.832 1.65e-06 ***
## windspeed     -75.62      16.82  -4.497 8.04e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1340 on 722 degrees of freedom
## Multiple R-squared:  0.5264, Adjusted R-squared:  0.5212 
## F-statistic: 100.3 on 8 and 722 DF,  p-value: < 2.2e-16

Q5:How accurately we can prdict the no of bike rentals, even future values of each of the relevant variables?

# Split the data into training and testing sets
set.seed(123) # For reproducibility
train_indices <- sample(1:nrow(bike_data), 0.8 * nrow(bike_data)) # 80% for training
train_data <- bike_data[train_indices, ]
test_data <- bike_data[-train_indices, ]

# Train the model on the training data
model <- lm(rentals ~ season + holiday + weekday + weather + temperature + realfeel + humidity + windspeed, data = train_data)

# Make predictions on the testing data
predictions <- predict(model, newdata = test_data)

# Evaluate accuracy
mse <- mean((test_data$rentals - predictions)^2)
mae <- mean(abs(test_data$rentals - predictions))
rsquared <- cor(test_data$rentals, predictions)^2

print(paste("Mean Squared Error (MSE):", mse))
## [1] "Mean Squared Error (MSE): 1853726.7141573"
print(paste("Mean Absolute Error (MAE):", mae))
## [1] "Mean Absolute Error (MAE): 1145.29374211083"
print(paste("R-squared:", rsquared))
## [1] "R-squared: 0.549971159165213"