-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAir-Pollution-Analysis.Rmd
294 lines (238 loc) · 7.85 KB
/
Air-Pollution-Analysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---
title: 'Data Analysis on Air Pollution level in Bangalore.'
---
```{r}
# Install packages, if necessary.
library(plyr)
library(ggplot2)
library(cluster)
library(lattice)
library(graphics)
library(grid)
library(gridExtra)
library(reshape2)
```
```{r}
# Download the datasets on your system and write in on your system's path to the datasets.
# Reading mutliple datasets for considerable amount of data.
setwd("D:/Data Science/Air-Pollution-Analysis-BLR/datasets")
data1 <- read.table("data_12.csv", header=T,sep=",")
data2 <- read.table("data_13.csv", header=T,sep=",")
data3 <- read.table("data_14.csv", header=T,sep=",")
data4 <- read.table("data_15.csv", header=T,sep=",")
data5 <- read.table("data_16.csv", header=T,sep=",")
data6 <- read.table("data_17.csv", header=T,sep=",")
data7 <- read.table("data_18.csv", header=T,sep=",")
data8 <- read.table("data_19.csv", header=T,sep=",")
data9 <- read.table("data_20.csv", header=T,sep=",")
data10 <- read.table("data_21.csv", header=T,sep=",")
data11 <- read.table("data_22.csv", header=T,sep=",")
data12 <- read.table("data_23.csv", header=T,sep=",")
data13 <- read.table("data_24.csv", header=T,sep=",")
data14 <- read.table("data_25.csv", header=T,sep=",")
data15 <- read.table("data_26.csv", header=T,sep=",")
data16 <- read.table("data_27.csv", header=T,sep=",")
data17 <- read.table("data_28.csv", header=T,sep=",")
data18 <- read.table("data_29.csv", header=T,sep=",")
data19 <- read.table("data_30.csv", header=T,sep=",")
data20 <- read.table("data_31.csv", header=T,sep=",")
```
```{r}
# Combining all the data frames into one big data frame for data analysis operations.
total<-rbind(data1,data2,data3,data4,data5,data6,data7,data8,data9,data10)
total<-rbind(total,data11,data12,data13,data14,data15,data16,data17,data18,data19,data20)
# Filtering data i.e. removing column ts_Unix as it is not required.
total$tsUnix<-NULL
# Printing the resultant data frame.
print(total)
```
```{r}
# Make all graphs with idnum at x-axis and all other pollutants at y-axis.
test_data_long <- melt(total, id="id")
# convert to long format
ggplot(data=test_data_long,
aes(x=id, y=value, colour=variable)) +
geom_line()
```
```{r}
# Doing linear regression plot of Co2 vs. idnum
fit<-lm(CO2~id,total)
total$CO2
fitted(fit)
residuals(fit)
plot(total$id,total$CO2, xlab="Id", ylab="Concentration of CO2")
abline(fit)
summary(fit)
```
```{r}
# Doing linear regression plot of CO vs. idnum
fit_CO<-lm(CO~id,total)
total$CO
fitted(fit_CO)
residuals(fit_CO)
plot(total$id,total$CO, xlab="Id", ylab="Concentration of CO")
abline(fit_CO)
summary(fit_CO)
```
```{r}
# Doing linear regression plot of NH3 vs. idnum
fit_NH3<-lm(NH3~id,total)
total$NH3
fitted(fit_NH3)
residuals(fit_NH3)
plot(total$id,total$NH3, xlab="Id", ylab="Concentration of NH3")
abline(fit_NH3)
summary(fit_NH3)
```
```{r}
# Doing linear regression plot of AN vs. idnum
fit_AN<-lm(AN~id,total)
total$AN
fitted(fit_AN)
residuals(fit_AN)
plot(total$id,total$AN, xlab="Id", ylab="Concentration of AN")
abline(fit_AN)
summary(fit_AN)
```
```{r}
# Doing polynomial regression plot to improve prediction for concentration of CO2.
# Similarily, polynomial regression can too be performed for other pollutants like AN, CO, NH3 etc.
# This (polynomial regression of degree 2) is the best prediction, to confirm please check higgher degrees like 3,4,5 etc.
fit2 <- lm(CO2 ~ id + I(id^2), data=total)
plot(total$id,total$CO2,xlab="Id ",ylab="CO2 level")
lines(total$id,fitted(fit2))
summary(fit2)
```
# Prediction of concentration of CO2 using any one value of idnum using different intervals.
```{r}
predict(fit, newdata=data.frame(id=1472029))
```
```{r}
predict(fit2, newdata=data.frame(id=1472029))
```
```{r}
predict(fit, newdata=data.frame(id=1472029), interval="pred")
```
```{r}
predict(fit2, newdata=data.frame(id=1472029), interval="pred")
```
```{r}
predict(fit, newdata=data.frame(id=1472029), interval="confidence")
```
```{r}
predict(fit2, newdata=data.frame(id=1472029), interval="confidence")
```
# Range prediction based on idnum values:
```{r}
# Downoad the datasets on your system and write in on your system's path to the datasets.
# Read the data into a table from the file.
sample<-read.table("D:/Data Science/Air-Pollution-Analysis-BLR/datasets/datasetPredictability.csv", header=T,sep=",")
traindata <- as.data.frame(sample[1:325,])
testdata <- as.data.frame(sample[326,])
traindata
```
```{r}
# Prediction of values of NH3 levels for a range of idnum values
fit2 <- lm(NH3~ idnum + I(idnum^2), data=traindata)
p1 <- ggplot(traindata, aes(x = idnum, y=NH3)) +geom_line() +geom_point()+geom_hline(aes(yintercept=12))
print(p1)
summary(fit2)
pred <- data.frame(idnum=300:400)
pred$NH3 <- predict(fit2, newdata=pred)
predict(fit2, newdata=pred, interval="pred")
predict(fit2, newdata=pred, interval="confidence")
p2<-p1 +geom_point(color="red", data=pred)
print(p2)
```
```{r}
# Prediction of values of CO2 levels for a range of idnum values
fit2 <- lm(CO2 ~ idnum + I(idnum^2), data=traindata)
p1 <- ggplot(traindata, aes(x = idnum, y=CO2)) +
geom_line() +
geom_point()+ geom_hline(aes(yintercept=0))
print(p1)
summary(fit2)
pred <- data.frame(idnum=300:400)
pred$CO2 <- predict(fit2, newdata=pred)
predict(fit2, newdata=pred, interval="pred")
predict(fit2, newdata=pred, interval="confidence")
p3<-p1 + geom_point(color="red", data=pred)
print(p3)
```
```{r}
# Prediction of values of CO levels for a range of idnum values
fit2 <- lm(CO ~ idnum + I(idnum^2), data=traindata)
p1 <- ggplot(traindata, aes(x = idnum, y=CO)) + geom_line() + geom_point()+ geom_hline(aes(yintercept=150))
print(p1)
summary(fit2)
pred <- data.frame(idnum=325:450)
pred$CO <- predict(fit2, newdata=pred)
predict(fit2, newdata=pred, interval="pred")
predict(fit2, newdata=pred, interval="confidence")
p2<-p1 + geom_point(color="red", data=pred)
print(p2)
```
```{r}
# Prediction of concentration values of pmx for a range of idnum values
# pmx = Particulate Matter X
fit2 <- lm(pmx ~ idnum + I(idnum^2), data=traindata)
p1 <- ggplot(traindata, aes(x = idnum, y=pmx)) +
geom_line() +
geom_point()+ geom_hline(aes(yintercept=0))
print(p1)
summary(fit2)
pred <- data.frame(idnum=300:400)
pred$pmx <- predict(fit2, newdata=pred)
predict(fit2, newdata=pred, interval="pred")
predict(fit2, newdata=pred, interval="confidence")
p2<-p1 +
geom_point(color="red", data=pred)
print(p2)
```
```{r}
# Prediction of concentration values of pmy for a range of idnum values
# pmy = Particulate Matter Y
fit2 <- lm(pmy ~ idnum + I(idnum^2), data=traindata)
p1 <- ggplot(traindata, aes(x = idnum, y=pmy)) +
geom_line() +
geom_point()+ geom_hline(aes(yintercept=0))
print(p1)
summary(fit2)
pred <- data.frame(idnum=300:400)
pred$pmy <- predict(fit2, newdata=pred)
predict(fit2, newdata=pred, interval="pred")
predict(fit2, newdata=pred, interval="confidence")
p2<-p1 +
geom_point(color="red", data=pred)
print(p2)
```
```{r}
# Prediction of concentration values of X_rssi for a range of idnum values
# X_rssi = Residual Soil Stability Index.
fit2 <- lm(X__rssi ~ idnum + I(idnum^2), data=traindata)
p1 <- ggplot(traindata, aes(x = idnum, y=X__rssi)) +
geom_line() +
geom_point()+ geom_hline(aes(yintercept=0))
print(p1)
summary(fit2)
pred <- data.frame(idnum=300:400)
pred$X__rssi <- predict(fit2, newdata=pred)
predict(fit2, newdata=pred, interval="pred")
predict(fit2, newdata=pred, interval="confidence")
p2<-p1 +
geom_point(color="red", data=pred)
print(p2)
```
```{r}
# Prediction of concentration values of AN for a range of idnum values
fit2 <- lm(AN ~ idnum + I(idnum^2), data=traindata)
p1 <- ggplot(traindata, aes(x = idnum, y=AN)) + geom_line() + geom_point() +geom_hline(aes(yintercept=0))
print(p1)
summary(fit2)
pred <- data.frame(idnum=300:400)
pred$AN <- predict(fit2, newdata=pred)
predict(fit2, newdata=pred, interval="pred")
predict(fit2, newdata=pred, interval="confidence")
p2<-p1 + geom_point(color="red", data=pred)
print(p2)
```