#Problem 1b #For the numerical variables, create a pairs plot that will show multiple data features. #For instance, create a scatter plot and box plot display similar to Textbook Figure 1.5, page 16. #Do you think this display allows you to observe the features of the data, i.e. outliers, variable associations, etc.? #Yes or no, and why?
# Set CRAN mirror
options(repos = c(CRAN = "https://cloud.r-project.org"))
# installs and load package
install.packages("car")
## Installing package into 'C:/Users/reese/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'car' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\reese\AppData\Local\Temp\RtmpQ7kLJG\downloaded_packages
library(car)
## Warning: package 'car' was built under R version 4.3.2
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.2
# loads csv
bikes.data <- read.csv("C:\\Users\\reese\\OneDrive\\Desktop\\Multivariate Statistical Methods - STAT 730\\R Work Directory\\SeoulBikes_Sp2024.csv")
# selects only numerical variables
numerical_vars <- bikes.data[, c("Temperature", "Humidity", "WindSpeed", "Visibility", "DewPointTemp", "SolarRadiation", "Rainfall", "Snowfall")]
# creates pairs plot with box plots on diagonals
spm(numerical_vars,
var.labels=c("Temperature", "Humidity", "WindSpeed", "Visibility", "Dew Point Temperature", "Solar Radiation", "Rainfall", "Snowfall"),
diagonal=list(method="boxplot"), smooth=F, regLine=F,
cex.axis=1.5, cex.labels=1.5, pch=20)
#Answer # Yes, I believe this display allows me to observe the features of the data. # For example, from the scatter plots I can see a linear relationship in some of the graphs as well as outliers. These are easily identifable. # For example, from the box plots I can also see outliers and the central tendency of this data, as well as IQR very easil to make determinations on skew.
#Problem 1c # Repeat 1b for each season. Report any interesting findings.
# seasons
Autumn <- bikes.data[bikes.data$Seasons == "Autumn", ]
Spring <- bikes.data[bikes.data$Seasons == "Spring", ]
Summer <- bikes.data[bikes.data$Seasons == "Summer", ]
Winter <- bikes.data[bikes.data$Seasons == "Winter", ]
# creates the pairs plot for each season's data
createPairsPlot <- function(season_data, season_name) {
numerical_vars <- season_data[, c("Temperature", "Humidity", "WindSpeed", "Visibility", "DewPointTemp", "SolarRadiation", "Rainfall", "Snowfall")]
spm(numerical_vars,
main=paste("Season:", season_name),
var.labels=c("Temperature", "Humidity", "WindSpeed", "Visibility", "Dew Point Temperature", "Solar Radiation", "Rainfall", "Snowfall"),
diagonal=list(method="boxplot"), smooth=FALSE, regLine=FALSE,
cex.axis=1.5, cex.labels=1.5, pch=20)
}
# plots for each season
createPairsPlot(Autumn, "Autumn")
createPairsPlot(Spring, "Spring")
createPairsPlot(Summer, "Summer")
createPairsPlot(Winter, "Winter")
#Answer # Interesting Findings: rainfall/snowfall, visibility, temperature/solar radiation # rainfall/snowfall: Have many outliers, especially in seasons where they are less common. For example, snowfall in spring and autumn shows significant outliers,indicating that snow is rare during these seasons. # visibility: less clear association with other variables, and the spread of the data points varies across seasons # temperature/solar radiation: within the summer plot, there seems to be a stronger association between temperature and solar radiation compared to other seasons
#Problem 1d # Comment on any features shown by your plots from parts 1b and 1c.
#Answer # 1b - temperature/solar radiation: positive correlation between these two variables, consistent across all seasons # 1b - humidity and dew point temperature: strong positive relationship is visible, indicating that as humidity increases, so does dew point temperature # 1c - winter: relationship between temperature and solar radiation is less pronounced than in other seasons # 1c - autumn: wind Speed has a wide distribution, possibly due to transition of seasons to colder months # 1c - spring: rainfall and snowfall are lower with few outliers
#Problem 1e #Construct the x,S and R arrays, and interpret the entries in R
# Calculates mean and standard deviation
mean_array <- colMeans(numerical_vars, na.rm = TRUE)
sd_array <- apply(numerical_vars, 2, sd, na.rm = TRUE)
# calculates the r arrays
correlation_matrix <- cor(numerical_vars, use = "complete.obs")
# prints arrays
mean_array
## Temperature Humidity WindSpeed Visibility DewPointTemp
## 1.273890e+01 5.855890e+01 1.758767e+00 1.440627e+03 4.058767e+00
## SolarRadiation Rainfall Snowfall
## 3.742603e-01 2.131507e-01 7.589041e-02
sd_array
## Temperature Humidity WindSpeed Visibility DewPointTemp
## 12.2948903 19.6030556 1.0041402 615.4837622 13.0149122
## SolarRadiation Rainfall Snowfall
## 0.3973348 1.4930997 0.4177733
correlation_matrix
## Temperature Humidity WindSpeed Visibility DewPointTemp
## Temperature 1.00000000 0.1069764 0.01737328 0.05912234 0.9204250
## Humidity 0.10697635 1.0000000 -0.33475583 -0.60417768 0.4788393
## WindSpeed 0.01737328 -0.3347558 1.00000000 0.21723339 -0.1209691
## Visibility 0.05912234 -0.6041777 0.21723339 1.00000000 -0.1682767
## DewPointTemp 0.92042497 0.4788393 -0.12096910 -0.16827666 1.0000000
## SolarRadiation 0.66410990 -0.2235402 0.12046922 0.19767371 0.5002651
## Rainfall 0.05498974 0.2694636 -0.07114900 -0.18711777 0.1388239
## Snowfall -0.23479163 0.1632826 -0.02496814 -0.16702163 -0.1566020
## SolarRadiation Rainfall Snowfall
## Temperature 0.6641099 0.05498974 -0.23479163
## Humidity -0.2235402 0.26946361 0.16328260
## WindSpeed 0.1204692 -0.07114900 -0.02496814
## Visibility 0.1976737 -0.18711777 -0.16702163
## DewPointTemp 0.5002651 0.13882387 -0.15660199
## SolarRadiation 1.0000000 -0.11199159 -0.16700977
## Rainfall -0.1119916 1.00000000 -0.01187189
## Snowfall -0.1670098 -0.01187189 1.00000000
#Answer #mean interpretation #temperature: average temperature is 12.74 C #humidity: average humidity is 58.5% #windspeed: average windspeed is 1.67 m/s #visibility: average visibility is 1440.63 (10m/s) #dew point temperature: average dew point temperature is 4.06 C #solar radiation: average solar radiation is 0.374 MJ/m2 #rainfall: average rainfall is 0.213 mm #snowfall: average snowfall is 0.076 mm #std interpretation #temperature: standard deviation of temperature is 12.29 C #humidity: standard deviation of humidity is 19.6%, moderate spread #windspeed: standard deviation of windspeed is 1 m/s, relatively stable #visibility: standard deviation of visibility is 615.48 (10m/s), wide variation #dew point temperature: standard deviation of dew point temperature is 13.01 C relatively variable #solar radiation: standard deviation of solar radiation is 0.397 MJ/m2, small variation #rainfall: standard deviation of rainfall is 1.49 mm, varies significantly #snowfall: standard deviation of snowfall is 0.42 mm, low variability #correlation #temperature and solar radiation: strong positive correlation (0.664), on warmer days, solar radiation tends to be higher. #windspeed and humidity: moderate negative correlation (-0.335), suggesting higher wind speeds might be associated with lower humidity #snowfall and temperature: moderate negative correlation (-0.235), which is expected as lower temperatures are associated with snowfall
#Problem 1f # Construct a scatter plot that shows Rented Bike Count, Temperature, and Seasons. # Do not use a 3D plot. Find a way to plot all three variables on a two-dimensional display. Check for outliers. # Can you distinguish any groups? Do you think temperature and seasons would be good predictors of rented bike count?
# installs and loads package
install.packages("ggplot2")
## Installing package into 'C:/Users/reese/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\reese\AppData\Local\Temp\RtmpQ7kLJG\downloaded_packages
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
# Assuming 'bikes.data' has a 'RentedBikeCount' and 'Seasons' column
# Create the scatter plot
ggplot(bikes.data, aes(x = Temperature, y = RentedBikeCount, color = Seasons)) +
geom_point() + # Use points to create the scatter plot
labs(title = "Rented Bike Count vs. Temperature and Seasons",
x = "Temperature",
y = "Rented Bike Count") +
theme_minimal() +
scale_color_brewer(palette = "Set1") # Use color to distinguish the seasons
#Answer # outliers: some are in the lower temperature range for Winter, where there are a few data points with relatively high bike rentals compared to the rest # outliers: a few points in the higher temperature range for Summer that deviate from the main cluster, unusually high bike rentals for the given temperature # distinguishable groups: winter shows a cluster with lower bike counts that increase as temperature rises # distinguishable groups: Summer and Autumn show higher bike counts at moderate temperatures # temperature and seasons as predictors of bike count: # temperature seems to have a non-linear relationship with bike counts. Bike counts increase with temperature up to a certain point before leveling off or decreasing, suggesting that extremely high temperatures may deter bike rentals. # Seasons appear to mediate this relationship. Example - a noticeable difference in bike counts at similar temperatures when comparing Summer and Winter # presence of groups suggest that both temperature and seasons could be good predictors for bike rental counts. The predictive power might not be linear across the entire range of temperatures.
#Problem 1g #Repeat part 1f using Rented Bike Count, Dew Point Temperature, and Rainfall.
# creates the scatter plot
ggplot(bikes.data, aes(x = DewPointTemp, y = RentedBikeCount, color = Rainfall)) +
geom_point(aes(size = Rainfall)) + # Use size to represent Rainfall levels
scale_color_gradient(low = "blue", high = "red") + # Use gradient color for Rainfall levels
labs(title = "Scatter Plot of Rented Bike Count vs. Dew Point Temperature and Rainfall",
x = "Dew Point Temperature",
y = "Rented Bike Count") +
theme_minimal() +
theme(legend.position = "right") # Adjust legend position
#Answer # outliers: observed at the lower and higher ends of the dew point temperature # outliers: large points (high Rainfall) are more scattered at lower rented bike counts - heavy rain might reduce bike rentals. # distinguishable groups: plot shows that most of the bike rentals occur at moderate dew point temperatures with low to no Rainfall. # dew point temperature and rainfall as predictors of bike count: # dew point temperature appears to have a non-linear relationship with bike count # rainfall seems to negatively impact bike rentals, indicated by the concentration of larger points with lower bike counts. # the relationship with dew point temperature and rainfall is not as clear cut, but it does indicate that there is an optimal range of dew point temperature where bike rentals are higher. rainfall, on the other hand, shows a clear trend where increased rainfall is associated with fewer bike rentals
#Problem 1h #Now, view the data in three dimensions (3D plot) using Rented Bike Count, Dew Point Temperature, and Rainfall again. Rotate the coordinate axes in various directions. Check for outliers. Can you distinguish any groups? Discuss the pros and cons of each type of plot to determine the data’s characteristics.
# installs and loads package
install.packages("scatterplot3d")
## Installing package into 'C:/Users/reese/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'scatterplot3d' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\reese\AppData\Local\Temp\RtmpQ7kLJG\downloaded_packages
library(scatterplot3d)
# creates the 3D plot
scatterplot3d(bikes.data$DewPointTemp, bikes.data$Rainfall, bikes.data$RentedBikeCount,
main=" Rented Bike Count, Dew Point Temperature, and Rainfall",
xlab="Dew Point Temperature",
ylab="Rainfall",
zlab="Rented Bike Count",
color=rainbow(length(bikes.data$Rainfall)),
pch=19)
#Answer # outliers: may be outliers at the higher end of the rented bike count, particularly at moderate dew point temperatures, a few points are far above the main data cluster # distinguishable groups: appears to be a trend where most bike rentals occur at lower rainfall # pros and cons: 2D # pros - easier to interpret for most audiences. Also can include up to four dimensions of data using x-y coordinates, color, and size. # cons - overplotting where data points overlap. Limited to visualizing relationships between two primary variables at a time. # pros and cons: 3D # pros - shows relationships involving three variables simultaneously. This is a more holistic view of how variables interact with each other in a dataset. # cons - difficult to interpret without interactive rotation and zooming. Overplotting can still occur, and certain angles can hide data
#Write one research question for the Seoul Bikes data that can be answered using a multivariate analysis technique. Explain why the selected multivariate method is the most appropriate. Hint: If the proper approach to answering the research question is not a multivariate analysis technique, find another research question. The method does not have to be among the ones we will learn in this class.
#Answer # How do weather conditions and seasonal changes predict the demand for bike rentals in Seoul, and can we sort certain days into different categories based on these conditions? An appropriate multivariate analysis technique for this research question would be a combination of Multiple Regression Analysis and Cluster Analysis. Multiple Regression Analysis would quantify the relationship between multiple independent variables like temperature, humidity, wind speed, visibility, dew point temperature, solar radiation, rainfall, snowfall, and seasons, alongside the dependent variable. This can handle multiple predictors at the same time, which is key since weather is multifaceted and its multiple aspects can be interrelated in affecting bike rental demand. Cluster analysis would be used after regression to group days into categories based on similar weather conditions and rental patterns. This can uncover hidden patterns or groupings in the data that might not be observable. It helps in sorting days into meaningful categories. Examples could be ‘high demand - good weather’, ‘low demand - bad weather’, ‘moderate demand - variable weather’, etc.