This dataset contains a hourly/daily summary for Szeged, Hungary area, between 2006 and 2016. With columns that include, time,summary,precipType,temperature,apparentTemperature,humidity,windSpeed,windBearing,visibility,loudCover,pressure. Link to Dataset: https://www.kaggle.com/datasets/budincsevity/szeged-weather
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.2 --
## v ggplot2 3.3.6 v purrr 0.3.4
## v tibble 3.1.8 v dplyr 1.0.10
## v tidyr 1.2.1 v stringr 1.4.1
## v readr 2.1.2 v forcats 0.5.2
## Warning: package 'ggplot2' was built under R version 4.1.3
## Warning: package 'tibble' was built under R version 4.1.3
## Warning: package 'tidyr' was built under R version 4.1.3
## Warning: package 'readr' was built under R version 4.1.3
## Warning: package 'dplyr' was built under R version 4.1.3
## Warning: package 'stringr' was built under R version 4.1.3
## Warning: package 'forcats' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Data <- read.csv("https://raw.githubusercontent.com/AldataSci/Multiple-Linear-Regression-attempt/main/weatherHistory.csv",header=TRUE)
head(Data)
## Formatted.Date Summary Precip.Type Temperature..C.
## 1 2006-04-01 00:00:00.000 +0200 Partly Cloudy rain 9.472222
## 2 2006-04-01 01:00:00.000 +0200 Partly Cloudy rain 9.355556
## 3 2006-04-01 02:00:00.000 +0200 Mostly Cloudy rain 9.377778
## 4 2006-04-01 03:00:00.000 +0200 Partly Cloudy rain 8.288889
## 5 2006-04-01 04:00:00.000 +0200 Mostly Cloudy rain 8.755556
## 6 2006-04-01 05:00:00.000 +0200 Partly Cloudy rain 9.222222
## Apparent.Temperature..C. Humidity Wind.Speed..km.h. Wind.Bearing..degrees.
## 1 7.388889 0.89 14.1197 251
## 2 7.227778 0.86 14.2646 259
## 3 9.377778 0.89 3.9284 204
## 4 5.944444 0.83 14.1036 269
## 5 6.977778 0.83 11.0446 259
## 6 7.111111 0.85 13.9587 258
## Visibility..km. Loud.Cover Pressure..millibars.
## 1 15.8263 0 1015.13
## 2 15.8263 0 1015.63
## 3 14.9569 0 1015.94
## 4 15.8263 0 1016.41
## 5 15.8263 0 1016.51
## 6 14.9569 0 1016.66
## Daily.Summary
## 1 Partly cloudy throughout the day.
## 2 Partly cloudy throughout the day.
## 3 Partly cloudy throughout the day.
## 4 Partly cloudy throughout the day.
## 5 Partly cloudy throughout the day.
## 6 Partly cloudy throughout the day.
## Clean out the null data types:
NewD <-Data %>%
select(-Daily.Summary,-Loud.Cover) %>%
filter(Precip.Type != "null")
## Convert precip.type into numerical representations:
NewD$Precip.Type <- as.character(NewD$Precip.Type)
NewD$Precip.Type[NewD$Precip.Type == "rain"] <- 0
NewD$Precip.Type[NewD$Precip.Type == "snow"] <- 1
NewD$Precip.Type <- as.integer(NewD$Precip.Type)
## wanted to change the weather summary into numerical representation as well but its too many entries to change..
unique(NewD$Summary)
## [1] "Partly Cloudy" "Mostly Cloudy"
## [3] "Overcast" "Foggy"
## [5] "Breezy and Mostly Cloudy" "Clear"
## [7] "Breezy and Partly Cloudy" "Breezy and Overcast"
## [9] "Humid and Mostly Cloudy" "Humid and Partly Cloudy"
## [11] "Windy and Foggy" "Windy and Overcast"
## [13] "Breezy and Foggy" "Windy and Partly Cloudy"
## [15] "Breezy" "Dry and Partly Cloudy"
## [17] "Windy and Mostly Cloudy" "Dangerously Windy and Partly Cloudy"
## [19] "Dry" "Windy"
## [21] "Humid and Overcast" "Light Rain"
## [23] "Drizzle" "Windy and Dry"
## [25] "Dry and Mostly Cloudy" "Breezy and Dry"
## [27] "Rain"
## Quadratic Variable (Double the WindSpeed)
Wind_spd <- NewD$Wind.Speed..km.h.^2
## Dichomtus vs QUantiative Interaction:
Precip_Wind <- Wind_spd * NewD$Precip.Type
## Making a model:
## Predict the temperature with humidity,wind_sped,Precip.wind
weather.lm <- lm(Temperature..C.~Wind_spd + Precip_Wind + Humidity,data=NewD)
summary(weather.lm)
##
## Call:
## lm(formula = Temperature..C. ~ Wind_spd + Precip_Wind + Humidity,
## data = NewD)
##
## Residuals:
## Min 1Q Median 3Q Max
## -53.320 -4.885 0.180 5.354 54.405
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.554e+01 9.444e-02 376.37 <2e-16 ***
## Wind_spd -3.570e-03 1.089e-04 -32.78 <2e-16 ***
## Precip_Wind -2.913e-02 3.070e-04 -94.90 <2e-16 ***
## Humidity -3.073e+01 1.182e-01 -259.89 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.985 on 95932 degrees of freedom
## Multiple R-squared: 0.4674, Adjusted R-squared: 0.4674
## F-statistic: 2.807e+04 on 3 and 95932 DF, p-value: < 2.2e-16
## Residual analysis This doesn't look good
plot(weather.lm$fitted.values, weather.lm$residuals, xlab="Fitted Values", ylab="Residuals", main="Residuals vs. Fitted", col = "red")
abline(h=0)
qqnorm(weather.lm$residuals, col = "blue")
qqline(weather.lm$residuals, col = "darkblue")
The model doesnโt look that good according to the residual analysis, we can see that the variability in the fitted residuals are all around the same values which is a sign that it is not a good model, in the qq plot we can see both tails skewing especially the left tail not being normally distributed.. The adjusted R2 explains 46% of the variability in temperature. We also see a very small F stat and all the predictors are significant. The equation according to the model is:
\[ Temperature = 35.54 -0.00357 * wind_spd -0.02913 * Precip_wind - 30.73 * Humidity \]