library(mosaic)
library(tidyverse)
library(pander)
library(DT)
library(plotly)
library(ggplot2)
library(car)

Background

For this linear regression model, the weather of Perth, Australia and Columbia, South Carolina from 2023 will be analyzed to see if the minimum temperature for one city can predict the maximum temperature of that same city. These two cities were chosen because Columbia, South Carolina is where I grew up and Perth, Australia is the city closest to the opposite side of the world from Columbia. The actual location of the opposite side of the world is in the middle of the ocean, Perth is the closest city on land to Columbia where weather is recorded.

For this analysis, the qualitative variables will be the cities and the quantitative variables will be the temperatures

The independent variable in this study will be the minimum temperature and the dependent variable will be the maximum temperature.

Hypothesis Testing:

For the hypothesis testing, a p-value of 0.05 will be used to evalute the results of the hypothsises.

\[ \alpha= 0.05 \]

Hypothesis:

Hypothesis Formulations

To determine if the weather patterns (specifically the relationship between Minimum and Maximum temperatures) differ between Perth and Columbia, we conduct two individual coefficient tests:

  1. Test for Difference in Slopes (Interaction Term):

\(H_0: \beta_3 = 0\) (The rate of temperature increase is identical in both cities; lines are parallel).

\(H_a: \beta_3 \neq 0\) (The rate of temperature increase differs between cities).

  1. Test for Difference in Intercepts (Location Term):

\(H_0: \beta_2 = 0\) (There is no baseline difference in maximum temperature between Perth and Columbia).

\(H_a: \beta_2 \neq 0\) (There is a baseline shift in maximum temperature between the two cities).

\[H_1: \text{At least one } \beta_i \neq 0\] \[ \underbrace{Y_i}_{\text{Max Temp}} = \overbrace{\beta_0 + \beta_1 \underbrace{X_{i1}}_{\text{Min Temp}}}^{\text{Perth Line}} + \overbrace{\beta_2 \underbrace{X_{i2}}_{\text{1 if Columbia}} + \beta_3 \underbrace{X_{i1} X_{i2}}_{\text{Interaction}}}^{\text{Columbia Adjustments to Line}} + \epsilon_i \]

library(GSODR) #run: install.packages("GSODR") 
# to get the GSODR package. You'll need this package to pull in your weather data.
load(system.file("extdata", "isd_history.rda", package = "GSODR"))

Graphic:

rexburg <- get_GSOD(years = 2023, station = "726818-94194")


CAE <- get_GSOD(years = 2023, station ="723100-13883")
Perth <- get_GSOD(years = 2023, station="946080-99999")
CPWeather <- rbind(CAE, Perth)

plot_ly(CPWeather, 
        x = ~MIN, 
        y = ~MAX, 
        color = ~NAME, 
        colors = c("powderblue", "darkseagreen"),
        type = "scatter", 
        mode = "markers",
        name = ~paste(NAME, "Points")) %>%
  add_lines(x = ~MIN, 
            y = ~fitted(lm(MAX ~ MIN * NAME, data = CPWeather)), 
            color = ~NAME,
            name = ~paste(NAME, "Trend")) %>%
  layout(title = "Daily MAX Temp vs. MIN Temp, 2023",
         xaxis = list(title = "Daily Low Temperature (C)"),
         yaxis = list(title = "Daily High Temperature (C)"))
datatable(CPWeather)

Graph Analysis:

Per the graph, the minimum temperature of Columbia, South Carolina seems to predicts the maximum temperature. Perth, Australia also seems to predict the maximum temperature based on the minimum temperature. The Perth data (displayed in Dark Sea Green) seems to have a steeper slope than the Columbia data does (displayed in Powder Blue).

Multiple Linear Regression Model:

WeatherModel <- lm(MAX ~ MIN * NAME, data=CPWeather)

pander(WeatherModel)
Fitting linear model: MAX ~ MIN * NAME
  Estimate Std. Error t value Pr(>|t|)
(Intercept) 17.89 0.3361 53.22 1.255e-252
MIN 0.6958 0.02378 29.26 6.801e-125
NAMEPERTH METRO -5.55 0.6239 -8.896 4.559e-18
MIN:NAMEPERTH METRO 0.3717 0.0463 8.026 4.043e-15

Analysis

The alpha level that was being used for this analysis is 0.05. For this analysis, the observed p-values were 2.2e^16, which is less than 0.05, meaning that we can reject the null hypothesis and observe that minimum temperature is a very strong predicter of maximum temperature.

For the p-value of MIN:NAMEPERTH it was 4.043^15, meaning that the relationship of minimum temperature and maximum temperature changes based on location.

It can also be observed that in Columbia, the temperature goes up .69 degree celcius for the max temperature for every degree celcius that the minimum temperature increases in the data. For Perth, the temperature increases 0.37 degree more than Columbia for every degree that the temperature rises in Columbia, explaining why Perth was observed to have a steeper slope of the line on the graph.

Assumptions

Residuals vs Fitted: The spread of the data points on the graph is consistent and it satisfies the homoscedasicity assumption, but there is a slight u shape in the data, however, this does not change the model because of how many samples of data are provided. There is slight non-linearity.

Q-Q Plot: The data points are normally distributed and close to the line with few outliers such as point 43 and 98, the normality assumption is met.

Residuals vs. Order: There are no obvious patterns and the points appear to be randomly scattered. The assumption for independence is met.

The assumptions are all met.

par(mfrow=c(1,3))
plot(WeatherModel, which=1)

qqPlot(WeatherModel$residuals)
## [1] 43 98
mtext(side=3,text="Q-Q Plot of Residuals")
plot(WeatherModel$residuals, type="b")
mtext(side=3, text="Residuals vs. Order")