setwd("C:/Users/nhath/OneDrive/Tài liệu/RStudio")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(car)
## Warning: package 'car' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
A <- read.csv("Airquality.csv")
A %>%
  ggplot(aes(Solar.R,Ozone))+
  geom_point()+
  stat_smooth(method = "lm",formula = y ~ poly(x,1))+
  stat_smooth(method = "lm",formula = y ~ poly(x,2))

A %>%
  ggplot(aes(Wind,Ozone))+
  geom_point()+
  stat_smooth(method = "lm",formula = y ~ poly(x,1))+
  stat_smooth(method = "lm",formula = y ~ poly(x,2))

A %>%
  ggplot(aes(Temp,Ozone))+
  geom_point()+
  stat_smooth(method = "lm",formula = y ~ poly(x,1))+
  stat_smooth(method = "lm",formula = y ~ poly(x,2))

Analyze the relationship of Ozone and each data (Solar.R, Wind, Temp) by creating a scatterplot with smoothed trend lines.

The scatterplot indicates a slight positive correlation between solar radiation and ozone levels. However, the trend line does not capture any distinct pattern in the data.

No significant correlation is observed between wind speed and ozone levels. The scatterplot shows scattered data points with no apparent trend.

Similarly, the scatterplot for temperature and ozone levels does not reveal a clear relationship. The trend lines fail to capture any discernible pattern in the data.

lm <- lm(Ozone~Solar.R+Wind+Temp,A)
summary(lm)
## 
## Call:
## lm(formula = Ozone ~ Solar.R + Wind + Temp, data = A)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -40.485 -14.219  -3.551  10.097  95.619 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -64.34208   23.05472  -2.791  0.00623 ** 
## Solar.R       0.05982    0.02319   2.580  0.01124 *  
## Wind         -3.33359    0.65441  -5.094 1.52e-06 ***
## Temp          1.65209    0.25353   6.516 2.42e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 21.18 on 107 degrees of freedom
## Multiple R-squared:  0.6059, Adjusted R-squared:  0.5948 
## F-statistic: 54.83 on 3 and 107 DF,  p-value: < 2.2e-16

Forming linear regression model, the adjusted R-squared is too small, indicating that the model explains only a small proportion of the variance in ozone levels.

A1 <- A %>% mutate(res=residuals(lm),fit = fitted.values(lm))
A1 %>% ggplot(aes(res)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

A1 %>% ggplot(aes(fit,res)) + geom_point()

Residual analysis was performed to assess the goodness of fit of the linear regression model. The histogram of residuals suggests that the residuals are approximately normally distributed. However, scatterplots of residuals against fitted values reveal an outlier.

A2 <- A1 %>% filter(res <= 50  & res >= -50)
A2
##     Ozone Solar.R Wind Temp Month Day          res        fit
## 1      41     190  7.4   67     5   1   7.95451746  33.045483
## 2      36     118  8.0   72     5   2   1.00129016  34.998710
## 3      12     149 12.6   74     5   3 -12.82281394  24.822814
## 4      18     313 11.5   62     5   4  -0.47522620  18.475226
## 5      23     299  8.6   65     5   7  -9.26143146  32.261431
## 6      19      99 13.8   59     5   8  25.94991879  -6.949919
## 7       8      19 20.1   61     5   9  37.43300539 -29.433005
## 8      16     256  9.7   69     5  12 -16.63056730  32.630567
## 9      11     290  9.2   66     5  13 -20.37498428  31.374984
## 10     14     274 10.9   68     5  14 -14.05493544  28.054935
## 11     18      65 13.2   58     5  15  26.63575698  -8.635757
## 12     14     334 11.5   64     5  16  -9.03564441  23.035644
## 13     34     307 12.0   66     5  17  10.94212135  23.057879
## 14      6      78 18.4   57     5  18  32.84485701 -26.844857
## 15     30     322 11.5   68     5  19   1.07383102  28.926169
## 16     11      44  9.7   62     5  20   2.61604815   8.383952
## 17      1       8  9.7   59     5  21  -0.27413188   1.274132
## 18     11     320 16.6   73     5  22  -9.06567669  20.065677
## 19      4      25  9.7   61     5  23  -1.59526773   5.595268
## 20     32      92 12.0   61     5  24  30.06401275   1.935987
## 21     23      13 12.0   67     5  28  15.87728189   7.122718
## 22     45     252 14.9   81     5  29  10.11827492  34.881725
## 23     37     279  7.4   76     5  31 -16.23835125  53.238351
## 24     29     127  9.7   82     6   7 -17.39091904  46.390919
## 25     71     291 13.8   90     6   9  15.24948527  55.750515
## 26     39     323 11.5   87     6  10 -21.37575487  60.375755
## 27     23     148  8.0   82     6  13 -30.31425664  53.314257
## 28     21     191 14.9   77     6  16  -3.62429745  24.624297
## 29     37     284 20.7   72     6  17  34.40768181   2.592318
## 30     20      37  9.2   65     6  18   5.41171790  14.588282
## 31     12     120 11.5   73     6  19 -13.10287436  25.102874
## 32     13     137 10.3   76     6  20 -22.07641269  35.076413
## 33     49     248  9.2   85     7   2 -11.25228481  60.252285
## 34     32     236  9.2   81     7   3 -20.92606608  52.926066
## 35     64     175  4.6   83     7   5  -3.91571592  67.915716
## 36     40     314 10.9   83     7   6 -15.22915270  55.229153
## 37     77     276  5.1   88     7   7  -3.55126441  80.551264
## 38     97     267  6.3   92     7   8  14.37905882  82.620941
## 39     97     272  5.7   92     7   9  12.07980109  84.920199
## 40     85     175  7.4   89     7  10  16.50578227  68.494218
## 41     10     264 14.3   73     7  12 -14.38298366  24.382984
## 42     27     175 14.9   81     7  13  -3.27553965  30.275540
## 43      7      48 14.3   80     7  15 -16.02638660  23.026387
## 44     48     260  6.9   81     7  16 -14.02902025  62.029020
## 45     35     274 10.3   82     7  17 -18.18439098  53.184391
## 46     61     285  6.3   84     7  18  -9.48096851  70.480969
## 47     79     187  5.1   87     7  19   5.42486101  73.575139
## 48     63     220 11.5   85     7  20  12.08995171  50.910048
## 49     16       7  6.9   74     7  21 -19.32976061  35.329761
## 50     80     294  8.6   86     7  24  13.34372036  66.656280
## 51    108     223  8.0   85     7  25  45.24292038  62.757080
## 52     20      81  8.6   82     7  26 -27.30612233  47.306122
## 53     52      82 12.0   86     7  27   9.35989587  42.640104
## 54     82     213  7.4   88     7  28  12.88469276  69.115307
## 55     50     275  7.4   86     7  29 -19.51999800  69.519998
## 56     64     253  7.4   83     7  30   0.75233371  63.247666
## 57     59     254  9.2   81     7  31   4.99716330  54.002837
## 58     39      83  6.9   81     8   1 -12.44077582  51.440776
## 59      9      24 13.8   81     8   2 -15.90958100  24.909581
## 60     16      77  7.4   82     8   3 -35.06714954  51.067150
## 61    122     255  4.0   89     8   7  37.38592463  84.614075
## 62     89     229 10.3   90     8   8  25.29079228  63.709208
## 63    110     207  8.0   90     8   9  39.93958526  70.060415
## 64     44     192 11.5   86     8  12  -6.88716468  50.887165
## 65     28     273 11.5   82     8  13 -21.12426082  49.124261
## 66     65     157  9.7   80     8  14  20.11864909  44.881351
## 67     22      71 10.3   77     8  16 -10.78034666  32.780347
## 68     59      51  6.3   79     8  17  10.77751410  48.222486
## 69     23     115  7.4   76     8  18 -20.42777449  43.427774
## 70     31     244 10.9   78     8  19 -11.78124685  42.781247
## 71     44     190 10.3   78     8  20   2.44891022  41.551090
## 72     21     259 15.5   77     8  21  -5.69194278  26.691943
## 73      9      36 14.3   72     8  22  -0.09179623   9.091796
## 74     45     212  9.7   79     8  24  -1.51939045  46.519390
## 75     73     215  8.0   86     8  26   9.06939218  63.930608
## 76     76     203  9.7   97     8  28   0.28132246  75.718678
## 77    118     225  2.3   94     8  29  21.25297256  96.747027
## 78     84     237  6.3   96     8  30  -3.43469512  87.434695
## 79     85     188  6.3   94     8  31   3.80069961  81.199300
## 80     96     167  6.9   91     9   1  23.01336551  72.986634
## 81     78     197  5.1   92     9   2  -4.43380945  82.433809
## 82     73     183  2.8   93     9   3 -17.91567410  90.915674
## 83     91     189  4.6   93     9   4   5.72586671  85.274133
## 84     47      95  7.4   87     9   5 -13.40438471  60.404385
## 85     32      92 15.5   84     9   6   3.73344536  28.266555
## 86     20     252 10.9   80     9   7 -26.56399739  46.563997
## 87     23     220 10.3   78     9   8 -20.34570748  43.345707
## 88     21     230 10.9   75     9   9 -15.98747986  36.987480
## 89     24     259  9.7   73     9  10 -15.41840071  39.418401
## 90     44     236 14.9   81     9  11  10.07540436  33.924596
## 91     21     259 15.5   76     9  12  -4.03984987  25.039850
## 92     28     238  6.3   77     9  13 -28.10475041  56.104750
## 93      9      24 10.9   71     9  14  -9.05606668  18.056067
## 94     13     112 11.5   71     9  15  -8.32012381  21.320124
## 95     46     237  6.9   78     9  16  -9.69686794  55.696868
## 96     18     224 13.8   67     9  17   4.25560176  13.744398
## 97     13      27 10.3   76     9  18 -15.49614779  28.496148
## 98     24     238 10.3   68     9  19  -3.90154898  27.901549
## 99     16     201  8.0   82     9  20 -40.48474791  56.484748
## 100    13     238 12.6   64     9  21  -0.62591734  13.625917
## 101    23      14  9.2   71     9  22  -0.12496600  23.124966
## 102    36     139 10.3   81     9  23  -7.45651842  43.456518
## 103     7      49 10.3   69     9  24 -11.24755039  18.247550
## 104    14      20 16.6   63     9  25  28.40142941 -14.401429
## 105    30     193  6.9   70     9  26  -9.84801870  39.848019
## 106    14     191 14.3   75     9  28  -9.32026641  23.320266
## 107    18     131  8.0   76     9  29 -24.38474915  42.384749
## 108    20     223 11.5   68     9  30  -3.00393057  23.003931

Create a new data set which filter out the outlier at both end of the histogram.

lm2 <- lm(Ozone~Solar.R + Wind + Temp + I(Wind^2) + I(Temp^2),A2)
summary(lm2)
## 
## Call:
## lm(formula = Ozone ~ Solar.R + Wind + Temp + I(Wind^2) + I(Temp^2), 
##     data = A2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -32.784  -8.364  -2.357   8.440  52.209 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 389.78906   79.01274   4.933 3.15e-06 ***
## Solar.R       0.05391    0.01561   3.454 0.000806 ***
## Wind         -6.66477    1.95203  -3.414 0.000919 ***
## Temp        -10.26905    2.15421  -4.767 6.23e-06 ***
## I(Wind^2)     0.21379    0.08354   2.559 0.011965 *  
## I(Temp^2)     0.07841    0.01411   5.556 2.22e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.13 on 102 degrees of freedom
## Multiple R-squared:  0.7766, Adjusted R-squared:  0.7656 
## F-statistic: 70.91 on 5 and 102 DF,  p-value: < 2.2e-16

To address the nonlinear relationship between environment factors and ozone levels, a polynomial regression model was fitted with wind speed^2 and temperature^2. The summary of the polynomial regression model suggests improved model performance compared to the linear model.

A2 %>% ggplot(aes(res)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

A2 %>% ggplot(aes(fit,res)) + geom_point()

cor(A)
##                Ozone     Solar.R        Wind       Temp        Month
## Ozone    1.000000000  0.34834169 -0.61249658  0.6985414  0.142885168
## Solar.R  0.348341693  1.00000000 -0.12718345  0.2940876 -0.074066683
## Wind    -0.612496576 -0.12718345  1.00000000 -0.4971897 -0.194495804
## Temp     0.698541410  0.29408764 -0.49718972  1.0000000  0.403971709
## Month    0.142885168 -0.07406668 -0.19449580  0.4039717  1.000000000
## Day     -0.005189769 -0.05775380  0.04987102 -0.0965458 -0.009001079
##                  Day
## Ozone   -0.005189769
## Solar.R -0.057753801
## Wind     0.049871017
## Temp    -0.096545800
## Month   -0.009001079
## Day      1.000000000

To check whether the first requirement that all independent variables are truly independent with each other, we use cor() to do that

shapiro.test(lm2$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  lm2$residuals
## W = 0.96757, p-value = 0.009704

The Shapiro test was conducted to test the normality of residuals from the polynomial regression model. The test result shows that the assumption of normality is good. The p-value is small so we reject the null hypothesis.

A2 %>%
  ggplot(aes(Ozone,Wind))+
  geom_point()+
  stat_smooth(method = "lm",formula = y ~ poly(x,1))+
  stat_smooth(method = "lm",formula = y ~ poly(x,2))

A2 %>%
  ggplot(aes(Ozone,Temp))+
  geom_point()+
  stat_smooth(method = "lm",formula = y ~ poly(x,1))+
  stat_smooth(method = "lm",formula = y ~ poly(x,2))

Additional scatterplots for showing the relationship between ozone levels and environment factors after filtering out outliers. However, no significant patterns were observed in the filtered data.

shapiro.test(lm2$res)
## 
##  Shapiro-Wilk normality test
## 
## data:  lm2$res
## W = 0.96757, p-value = 0.009704

A Shapiro test on the residuals of the polynomial regression model. The Shapiro test tests whether the residuals are normally distributed. Since the p-value is small, we reject the null hypothesis.

While polynomial regression models show some improvement, they still fail to explain a substantial portion of the variance in ozone levels. Further research may be required to identify additional factors influencing ozone levels and improve predictive models.