-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrop Yield Code.R
297 lines (225 loc) · 11.4 KB
/
Crop Yield Code.R
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
295
296
rm(list=ls()) # this removes old files that were previously loaded
setwd("~/Desktop/dfsf") # all files must be in the
# designated folder
# read in the myfunctions.R file
source("myfunctions-1.R")
library(tidyverse)
#
# Read in data. Most datafiles will have a header row,
# which are column headings
data <- read.csv("~/Desktop/REX-Gotwals/archive/yield_df.csv", header=T)
head(data)
names(data)
India <- filter(data, Area == "India")
Brazil <- filter(data, Area == "Brazil")
Kenya <- filter(data, Area == "Kenya")
Netherlands <- filter(data, Area == "Netherlands")
hist(data$Year)
hist(data$avg_temp)
hist(data$average_rain_fall_mm_per_year)
# Function to add correlation coefficients
panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...) {
usr <- par("usr")
on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
Cor <- abs(cor(x, y)) # Remove abs function if desired
txt <- paste0(prefix, format(c(Cor, 0.123456789), digits = digits)[1])
if(missing(cex.cor)) {
cex.cor <- 0.4 / strwidth(txt)
}
text(0.5, 0.5, txt,
cex = 1 + cex.cor * Cor) # Resize the text by level of correlation
}
# Plotting the correlation matrix
pairs(~ India$Year + India$hg.ha_yield + India$pesticides_tonnes + India$avg_temp,
data = India,
upper.panel = panel.cor, # Correlation panel
lower.panel = panel.smooth)
simpleFit <- lm(India$avg_temp~India$Year)
plot(India$avg_temp~India$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
# Fair to assume that temperatures have been rising.
simpleFit <- lm(India$pesticides_tonnes~India$Year)
plot(India$pesticides_tonnes~India$Year, main="Plot of Year vs. Pesticide", xlab = "Year", ylab="Pesticide in Tonnes")
abline(simpleFit)
# Clear decrease
#IndiaMaize
IndiaMaize <- filter(India, Item == "Maize")
simpleFit <- lm(IndiaMaize$hg.ha_yield~IndiaMaize$Year)
plot(IndiaMaize$hg.ha_yield~IndiaMaize$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
simpleFit <- lm(IndiaMaize$hg.ha_yield~IndiaMaize$avg_temp)
plot(IndiaMaize$hg.ha_yield~IndiaMaize$avg_temp, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
multiFit <- lm(IndiaMaize$hg.ha_yield~IndiaMaize$avg_temp + IndiaMaize$Year)
summary(multiFit)
BIC(lm(IndiaMaize$hg.ha_yield~1))
BIC(lm(IndiaMaize$hg.ha_yield~IndiaMaize$avg_temp))
BIC(lm(IndiaMaize$hg.ha_yield~IndiaMaize$pesticides_tonnes))
BIC(lm(IndiaMaize$hg.ha_yield~IndiaMaize$Year))
BIC(lm(IndiaMaize$hg.ha_yield~IndiaMaize$avg_temp + IndiaMaize$pesticides_tonnes))
#IndiaWheat
IndiaWheat <- filter(India, Item == "Wheat")
simpleFit <- lm(IndiaWheat$hg.ha_yield~IndiaWheat$Year)
plot(IndiaWheat$hg.ha_yield~IndiaWheat$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
simpleFit <- lm(IndiaWheat$hg.ha_yield~IndiaWheat$avg_temp)
plot(IndiaWheat$hg.ha_yield~IndiaWheat$avg_temp, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
multiFit <- lm(IndiaWheat$hg.ha_yield~IndiaWheat$avg_temp + IndiaWheat$Year)
summary(multiFit)
BIC(lm(IndiaWheat$hg.ha_yield~1))
BIC(lm(IndiaWheat$hg.ha_yield~IndiaWheat$avg_temp))
BIC(lm(IndiaWheat$hg.ha_yield~IndiaWheat$pesticides_tonnes))
BIC(lm(IndiaWheat$hg.ha_yield~IndiaWheat$Year))
BIC(lm(IndiaWheat$hg.ha_yield~IndiaWheat$avg_temp + IndiaWheat$pesticides_tonnes))
#BRAZIL
pairs(~ Brazil$Year + Brazil$hg.ha_yield + Brazil$pesticides_tonnes + Brazil$avg_temp,
data = Brazil,
upper.panel = panel.cor, # Correlation panel
lower.panel = panel.smooth)
simpleFit <- lm(Brazil$avg_temp~Brazil$Year)
plot(Brazil$avg_temp~Brazil$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
# Fair to assume that temperatures have been rising.
simpleFit <- lm(Brazil$pesticides_tonnes~Brazil$Year)
plot(Brazil$pesticides_tonnes~Brazil$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
# Clear decrease
#BrazilMaize
BrazilMaize <- filter(Brazil, Item == "Maize")
simpleFit <- lm(BrazilMaize$hg.ha_yield~BrazilMaize$Year)
plot(BrazilMaize$hg.ha_yield~BrazilMaize$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
simpleFit <- lm(BrazilMaize$hg.ha_yield~BrazilMaize$avg_temp)
plot(BrazilMaize$hg.ha_yield~BrazilMaize$avg_temp, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
multiFit <- lm(BrazilMaize$hg.ha_yield~BrazilMaize$avg_temp + BrazilMaize$Year)
summary(multiFit)
BIC(lm(BrazilMaize$hg.ha_yield~1))
BIC(lm(BrazilMaize$hg.ha_yield~BrazilMaize$avg_temp))
BIC(lm(BrazilMaize$hg.ha_yield~BrazilMaize$pesticides_tonnes))
BIC(lm(BrazilMaize$hg.ha_yield~BrazilMaize$Year))
BIC(lm(BrazilMaize$hg.ha_yield~BrazilMaize$avg_temp + BrazilMaize$pesticides_tonnes))
#BrazilWheat
BrazilWheat <- filter(Brazil, Item == "Wheat")
simpleFit <- lm(BrazilWheat$hg.ha_yield~BrazilWheat$Year)
plot(BrazilWheat$hg.ha_yield~BrazilWheat$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
summary(simpleFit)
simpleFit <- lm(BrazilWheat$hg.ha_yield~BrazilWheat$avg_temp)
plot(BrazilWheat$hg.ha_yield~BrazilWheat$avg_temp, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
multiFit <- lm(BrazilWheat$hg.ha_yield~BrazilWheat$pesticides_tonnes)
summary(multiFit)
BIC(lm(BrazilWheat$hg.ha_yield~1))
BIC(lm(BrazilWheat$hg.ha_yield~BrazilWheat$avg_temp))
BIC(lm(BrazilWheat$hg.ha_yield~BrazilWheat$pesticides_tonnes))
BIC(lm(BrazilWheat$hg.ha_yield~BrazilWheat$Year))
BIC(lm(BrazilWheat$hg.ha_yield~BrazilWheat$avg_temp + BrazilWheat$pesticides_tonnes))
#KENYA
pairs(~ Kenya$Year + Kenya$hg.ha_yield + Kenya$pesticides_tonnes + Kenya$avg_temp,
data = Kenya,
upper.panel = panel.cor, # Correlation panel
lower.panel = panel.smooth)
simpleFit <- lm(Kenya$avg_temp~Kenya$Year)
plot(Kenya$avg_temp~Kenya$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
# Fair to assume that temperatures have been rising.
simpleFit <- lm(Kenya$pesticides_tonnes~Kenya$Year)
plot(Kenya$pesticides_tonnes~Kenya$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
# Clear decrease
#KenyaMaize
KenyaMaize <- filter(Kenya, Item == "Maize")
simpleFit <- lm(KenyaMaize$hg.ha_yield~KenyaMaize$Year)
plot(KenyaMaize$hg.ha_yield~KenyaMaize$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
simpleFit <- lm(KenyaMaize$hg.ha_yield~KenyaMaize$avg_temp)
plot(KenyaMaize$hg.ha_yield~KenyaMaize$avg_temp, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
multiFit <- lm(KenyaMaize$hg.ha_yield~KenyaMaize$avg_temp + KenyaMaize$Year)
summary(multiFit)
BIC(lm(KenyaMaize$hg.ha_yield~1))
BIC(lm(KenyaMaize$hg.ha_yield~KenyaMaize$avg_temp))
BIC(lm(KenyaMaize$hg.ha_yield~KenyaMaize$pesticides_tonnes))
BIC(lm(KenyaMaize$hg.ha_yield~KenyaMaize$Year))
BIC(lm(KenyaMaize$hg.ha_yield~KenyaMaize$avg_temp + KenyaMaize$pesticides_tonnes))
#KenyaWheat
KenyaWheat <- filter(Kenya, Item == "Wheat")
simpleFit <- lm(KenyaWheat$hg.ha_yield~KenyaWheat$Year)
plot(KenyaWheat$hg.ha_yield~KenyaWheat$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
summary(simpleFit)
simpleFit <- lm(KenyaWheat$hg.ha_yield~KenyaWheat$avg_temp)
plot(KenyaWheat$hg.ha_yield~KenyaWheat$avg_temp, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
multiFit <- lm(KenyaWheat$hg.ha_yield~KenyaWheat$pesticides_tonnes)
summary(multiFit)
BIC(lm(KenyaWheat$hg.ha_yield~1))
BIC(lm(KenyaWheat$hg.ha_yield~KenyaWheat$avg_temp))
BIC(lm(KenyaWheat$hg.ha_yield~KenyaWheat$pesticides_tonnes))
BIC(lm(KenyaWheat$hg.ha_yield~KenyaWheat$Year))
BIC(lm(KenyaWheat$hg.ha_yield~KenyaWheat$avg_temp + KenyaWheat$pesticides_tonnes))
#NETHERLANDS
pairs(~ Netherlands$Year + Netherlands$hg.ha_yield + Netherlands$pesticides_tonnes + Netherlands$avg_temp,
data = Netherlands,
upper.panel = panel.cor, # Correlation panel
lower.panel = panel.smooth)
simpleFit <- lm(Netherlands$avg_temp~Netherlands$Year)
plot(Netherlands$avg_temp~Netherlands$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
# Fair to assume that temperatures have been rising.
simpleFit <- lm(Netherlands$pesticides_tonnes~Netherlands$Year)
plot(Netherlands$pesticides_tonnes~Netherlands$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
# Clear decrease
#NetherlandsMaize
NetherlandsMaize <- filter(Netherlands, Item == "Maize")
simpleFit <- lm(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$Year)
plot(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
simpleFit <- lm(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$avg_temp)
plot(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$avg_temp, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
multiFit <- lm(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$avg_temp + NetherlandsMaize$Year)
summary(multiFit)
BIC(lm(NetherlandsMaize$hg.ha_yield~1))
BIC(lm(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$avg_temp))
BIC(lm(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$pesticides_tonnes))
BIC(lm(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$Year))
BIC(lm(NetherlandsMaize$hg.ha_yield~NetherlandsMaize$avg_temp + NetherlandsMaize$pesticides_tonnes))
#NetherlandsWheat
NetherlandsWheat <- filter(Netherlands, Item == "Wheat")
simpleFit <- lm(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$Year)
plot(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$Year, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
summary(simpleFit)
simpleFit <- lm(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$avg_temp)
plot(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$avg_temp, main="Plot of Year vs. Temperature", xlab = "Year", ylab="Temperature")
abline(simpleFit)
multiFit <- lm(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$pesticides_tonnes)
summary(multiFit)
BIC(lm(NetherlandsWheat$hg.ha_yield~1))
BIC(lm(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$avg_temp))
BIC(lm(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$pesticides_tonnes))
BIC(lm(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$Year))
BIC(lm(NetherlandsWheat$hg.ha_yield~NetherlandsWheat$avg_temp + NetherlandsWheat$pesticides_tonnes))
NetherlandsMaize$Area <- NULL
NetherlandsMaize$Item <- NULL
NetherlandsMaize$average_rain_fall_mm_per_year <- NULL
data$Area <- NULL
data$Item <- NULL
data$average_rain_fall_mm_per_year <- NULL
pca <- prcomp(data, scale=TRUE)
summary(pca)
pca.variance <- pca$sdev^2
pca.variance.per <- round(pca.variance / sum(pca.variance)*100, 1)
barplot(pca.variance.per, main="Scree Plot of PCs vs Variance", xlab="Principal Components", ylab="Variance", ylim=c(0, 100))
plot(pca$x[,1], pca$x[,2], xlab="PC1", ylab="PC2", main="PC1 vs. PC2")
pca.data <- data.frame(Sample=rownames(pca$x), X=pca$x[,1], Y=pca$x[,2])
ggplot(data=pca.data, aes(x=X, y=Y, label=Sample)) + geom_text() + xlab(paste("PC1 - ", pca.variance.per[1], "%", sep="")) + ylab(paste("PC2 - ", pca.variance.per[2], "%", sep="")) + theme_bw() + ggtitle("PC1 vs. PC2")
loading_scores <- pca$rotation[,1]
drug_scores <- abs(loading_scores)
drug_scores_ranked <- sort(drug_scores, decreasing=T)
names(drug_scores_ranked)