2024-03-17

Observed Dataset - Air Quality

This is a dataset recording some of New York’s weather statistics recorded from May 1, 1973 to September 30, 1973. The data includes date of observation, Ozone (in ppb), solar radiation (in lang), wind speed (in mph), and temperature (in Farenheit).

Here are the first 6 observations of the dataset

##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6

Simple Linear Regression Formulas

Estimation Model based off Sample

  • \(\hat{y} = \hat{\beta_0} + \hat{\beta_1} \hat{x}\)

Model based off Population

  • \({Y} = {\beta_0} + {\beta_1} {X}\)

For an explanation, \({Y}\) is the dependent variable for the population while \(\hat{y}\) is the dependent variable for a sample. Likewise, \({X}\) is the independent variable for the population while \(\hat{x}\) is the independent variable for a sample. \({\beta_0}\) is the y-intercept of the plot for the population, while \(\hat{\beta_0}\) is the y-intercept of the plot for a sample. However, \({\beta_1}\) is the correlation coefficient of the plot for the population between the independent and dependent variable, while \(\hat{\beta_1}\) is the correlation coefficient of the plot for the sample between the independent and dependent variable.

How to Find Line of Best Fit by Hand

Calculators take care of the job for you, but it is still good to know how to do it manually. First, find the means of the independent and dependent variables, which will make a point on the line.

Mean for x: \(\bar{x} = \dfrac{\sum_{i=1}^{n} x_i}{n}\). Mean for y: \(\bar{y} = \dfrac{\sum_{i=1}^{n} y_i}{n}\)

Second, find \(\hat{\beta_1}\) - compare each point to the means, multiply those means, add all those products together, and divide that answer by the sum of the squared differences between x and \(\bar{x}\).

\(\hat{\beta_1} = \dfrac{\sum_{i=1}^{n}(x_i-\bar{x}) \cdot (y_i-\bar{y})}{\sum_{i=1}^{n}(x_i-\bar{x})^2}\)

Now that you have \(\hat{\beta_1}\) and a point of the line (\(\bar{x}\),\(\bar{y}\)), you can solve \(\hat{\beta_0}\). \(\hat{\beta_0} = \bar{y} - \hat{\beta_1}\bar{x}\)

Now you have your line of best fit in the form of \(\hat{y} = \hat{\beta_0} + \hat{\beta_1} \hat{x}\)

Linear Regression with Plotly

One would assume that a higher Ozone ppb would lead to hotter temperatures due to the fact that it traps heat. Let’s do a linear regression to see a trend.

First Linear Regression with ggplot

Someone would usually associate wind as something that makes the weather cooler, but wind in summer hot weather sometimes feels like wind is just making it worse. Let’s draw a line of best fit for every month to see if windy and hot weather are ever positvely correlated in the same month.

Code for Plot and Regression from Last Slide (Slide 6)

month_model = function(month) {
  lm(Temp ~ Wind, data = subset(cleaned_airqual, Month == month))
}
month_models = sapply(5:9, month_model)

ggplot(cleaned_airqual, aes(x = Wind, y = Temp, color = factor(Month))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, aes(x = Wind)) +  
  labs(title = "Scatterplot and Linear Regression of Temperature vs. 
       Wind Speed by Month",
       x = "Wind Speed in mph (Miles per Hour)", 
       y = "Temperature in Farenheit", color = "Month")

Second Linear Regression with ggplot

One would also assume that a higher solar radiation leads to hotter temperatures due to the suns effect on both phenomena. Let’s draw a scatterplot and linear regression for temperature vs solar radiation to see if there is a defining correlation.