-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05_cvi_ndvi_stats_need_to_update.R
458 lines (324 loc) · 14.7 KB
/
05_cvi_ndvi_stats_need_to_update.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#05_cvi_ndvi_stats.R
#This script was written by J. Atkins 2015, 2016 (jeffatkins@virginia.edu or @atkinsjeff)
#https://github.com/atkinsjeff
#
#This script includes the linear regression model and all statistics from the manuscript
#I have left a lot of old code in here because honestly I was learning as I went along and I just kept it here.
##################
#LANDSAT linear regression change model
#each cell is recalculated to a change in time.
require(gplots)
library(broom)
library(magrittr)
require(plyr)
library(dplyr)
require(tidyr)
require(Hmisc)
require(psych)
####NEED TO ORDER SCRIPT BY PAPER
summary(ndvi.86)
cellStats(ndvi.86, stat = "mean")
summary(ndvi.90)
cellStats(ndvi.90, stat = "mean")
summary(ndvi.95)
cellStats(ndvi.95, stat = "mean")
summary(ndvi.05)
cellStats(ndvi.05, stat = "mean")
summary(ndvi.09)
cellStats(ndvi.09, stat = "mean")
summary(ndvi.11)
cellStats(ndvi.11, stat = "mean")
######### Linear regression scripts
#### NDVI
ndvi.stack <- bind_rows(c(df.86, df.90, df.95, df.05, df.09, df.11))
ndvi.stack <- data.frame(ndvi.stack)
stack <- ndvi.stack
raw.stack <- stack
stack <- gather(stack, "rawyear", "ndvi", 3:8)
library(stringr)
stack$year <- as.numeric(unlist(str_extract_all(stack$rawyear, '\\d+')))
#ndvi
head(df.86.11)
ndvi.86.11.positive <- subset(df.86.11, diff > 0)
ndvi.86.11.negative <- subset(df.86.11, diff <= 0)
# evi
head(e.86.11)
summary(e.86.11)
evi.86.11.positive <- subset(e.86.11, diff > 0)
evi.86.11.negative <- subset(e.86.11, diff <= 0)
head(s.86.11)
summary(s.86.11)
savi.86.11.positive <- subset(s.86.11, diff > 0)
savi.86.11.negative <- subset(s.86.11, diff <= 0)
########## Linear regressions with pipes
library(broom)
library(magrittr)
library(dplyr)
require(tidyr)
#NDVI
lm.ndvi.pipe =
stack %>%
group_by(x,y) %>%
do({model = lm(ndvi~year, data=.) # create your model
data.frame(tidy(model), # get coefficient info
glance(model))}) # get model info
lm.ndvi.pipe %>%
select(x, y, term, estimate, p.value)
lm.ndvi.pipe %>%
select(x, y, term, estimate, adj.r.squared, p.value) %>%
spread(term, estimate)
lm.ndvi.pipe.clean <- subset(lm.ndvi.pipe[,c(1,2,3,4,7,9)])
lm.ndvi.pipe.clean <- subset(lm.ndvi.pipe.clean, term == "year")
plot(lm.ndvi.pipe.clean$p.value)
###3 Trying raster p values
temp.ndvi <- lm.ndvi.pipe.clean[,c(1,2,4,5)]
temp.ndvi$change <- temp.ndvi$estimate * 25
temp.ras <- lm.ndvi.pipe.clean[,c(1,2,5)]
rasterNDVI.p <- rasterFromXYZ(temp.ras)
#plotting p values
breakpoints <- c(0, 0.01, 0.05,1)
colors <- c("dark red","red","light grey")
plot(rasterNDVI.p,breaks=breakpoints,col=colors )+
mtext("P value")
#change
temp.ras.change <- temp.ndvi[,c(1,2, 5)]
rasterNDVI.change <- rasterFromXYZ(temp.ras.change)
colfunc <- colorRampPalette(c("brown", "white", "dark green"))
colfunc(12)
breakpoints.change <- c(-0.6, -0.5, -0.4, -0.3, -0.2, 0, 0.2, 0.4, 0.6)
plot(rasterNDVI.change, col = colfunc(10), breaks = seq(-0.5, 0.5, by = 0.1))+
mtext("NDVI change")
lm.ndvi.ras <- raster(temp.ndvi, "p.value")
#putting in all the landscape variables elevation class and distance to stream and aspect
lm.ndvi.elev <- merge(temp.ndvi, elev_classes, by = c("x","y"))
lm.ndvi.elev <- merge(lm.ndvi.elev, stream_distance, by = c("x","y"))
lm.ndvi.elev$elev <- as.factor(lm.ndvi.elev$elev)
lm.ndvi.elev <- merge(lm.ndvi.elev, weimer_aspect, by = c("x", "y"))
lm.ndvi.elev <- merge(lm.ndvi.elev, elev_raw, by = c("x", "y"))
# let's look at some stats
# How many p value less than 0.05
(sum(temp.ndvi$p.value <= 0.05) / 4153)
(sum(temp.ndvi$p.value <= 0.01) / 4153)
#adding a factor column based on p.value
lm.ndvi.elev = within(lm.ndvi.elev, {
p.class = ifelse(p.value <= 0.05, "SIGNIFICANT", "NOT SIGNIFICANT")
})
lm.ndvi.elev$p.class <- as.factor(lm.ndvi.elev$p.class)
summary(lm.ndvi.elev)
#write.csv(lm.ndvi.elev, file="lm_ndvi_output_may_2017.csv")
ndvi.positive <- subset(lm.ndvi.elev, change > 0)
ndvi.negative <- subset(lm.ndvi.elev, change <= 0)
###################
# LANDESCAPE ANALYSIS setion
ndvi.df <- ndvi.elev
colnames(ndvi.df)[colnames(ndvi.df) == "ndvi1986"] <- "ndvi.86"
colnames(ndvi.df)[colnames(ndvi.df) == "b1990_ndvi"] <- "ndvi.90"
colnames(ndvi.df)[colnames(ndvi.df) == "b1995_ndvi"] <- "ndvi.95"
colnames(ndvi.df)[colnames(ndvi.df) == "b2005_ndvi"] <- "ndvi.05"
colnames(ndvi.df)[colnames(ndvi.df) == "b2009_ndvi"] <- "ndvi.09"
colnames(ndvi.df)[colnames(ndvi.df) == "b2011_ndvi"] <- "ndvi.11"
x <- merge(ndvi.df, stream_distance, by = c("x", "y"))
x3 <- merge(x, weimer_aspect, by = c("x", "y"))
x4 <- merge(x3, elev_raw, by = c("x", "y"))
x4$elev <-as.factor(x4$elev)
class_ndvi <- x4
class_ndvi$elev.raw[class_ndvi$elev.raw == 0] <- NA
class_ndvi <- na.omit(class_ndvi)
###### Elevation
###!!!!!!!!! this section dependent on elevation_time.R script
####ELEVATION ANALYSIS ONE using the output from the linear regression, lm.pipes model
lm.ndvi.high <- subset(lm.ndvi.elev, elev == "HIGH")
lm.ndvi.mid <- subset(lm.ndvi.elev, elev == "MID")
lm.ndvi.low <- subset(lm.ndvi.elev, elev == "LOW")
# Uses previous function to summarize elevation linear regression data
sapply(lm.ndvi.high[,ind], my.summary)
sapply(lm.ndvi.mid[,ind], my.summary)
sapply(lm.ndvi.low[,ind], my.summary)
# ############# NDVI
# #merging w/ elev classes to
ndvi.elev <- merge(ndvi.stack, elev_classes, by = c("x", "y"))
aggregate(ndvi ~ elev + year, data = ndvi.elev, FUN=mean)
ndvi.high <- subset(ndvi.elev, elev == "HIGH")
ndvi.mid <- subset(ndvi.elev, elev == "MID")
ndvi.low <- subset(ndvi.elev, elev == "LOW")
sapply(ndvi.high[,ind], my.summary)
sapply(ndvi.mid[,ind], my.summary)
sapply(ndvi.low[,ind], my.summary)
#
# ############ G+W
####Analysis of variance script
####################
#first test normality assumptions (they are good)
shapiro.test(class_ndvi$ndvi.86)
shapiro.test(class_ndvi$ndvi.11)
#ANOVA of NDVI (1986 and 2011) to aspect.class, with no covariant#ANOVA for raw differences in elevation and aspect
lm.elev.fit.86 <- aov(ndvi.86 ~ elev + aspect.class + elev * aspect.class, data = class_ndvi, na.action = na.omit)
lm.elev.fit.90 <- aov(ndvi.90 ~ elev + aspect.class + elev * aspect.class, data = class_ndvi, na.action = na.omit)
lm.elev.fit.95 <- aov(ndvi.95 ~ elev + aspect.class + elev * aspect.class, data = class_ndvi, na.action = na.omit)
lm.elev.fit.05 <- aov(ndvi.05 ~ elev + aspect.class + elev * aspect.class, data = class_ndvi, na.action = na.omit)
lm.elev.fit.09 <- aov(ndvi.09 ~ elev + aspect.class + elev * aspect.class, data = class_ndvi, na.action = na.omit)
lm.elev.fit.11 <- aov(ndvi.11 ~ elevelev + aspect.class + elev * aspect.class, data = class_ndvi, na.action = na.omit)
sum(is.na(class_ndvi$elev))
summary(lm.elev.fit.86)
summary(lm.elev.fit.11)
TukeyHSD(lm.elev.fit.86)
TukeyHSD(lm.elev.fit.11)
require(psych)
#Using the pscyh packate to describe.by elev
describeBy(class_ndvi, class_ndvi$elev)
#Using the pscyh packate to describe.by aspect.class
describeBy(class_ndvi, class_ndvi$aspect.class)
#ANOVA on NDVI change by elevation and aspect.class from trend analysis
shapiro.test(lm.ndvi.elev$change)
lm.trend.ndvi.fit <- aov(change ~ elev * aspect.class, data = lm.ndvi.elev)
summary(lm.trend.ndvi.fit)
TukeyHSD(lm.trend.ndvi.fit)
#looking at means from each 1986, and 2011 and trend
aggregate(ndvi.86 ~ elev + aspect.class, data = class_ndvi, FUN = mean)
aggregate(ndvi.86 ~ aspect.class, data = class_ndvi, FUN = sd)
aggregate(ndvi.11 ~ elev + aspect.class, data = class_ndvi, FUN = mean)
aggregate(ndvi.11 ~ aspect.class, data = class_ndvi, FUN = mean)
aggregate(change ~ elev + aspect.class, data = lm.ndvi.elev, FUN = mean)
aggregate(change ~ aspect.class, data = lm.ndvi.elev, FUN = mean)
#looking at means by elevation
aggregate(ndvi.86 ~ elev, data = class_ndvi, FUN = mean)
aggregate(ndvi.11 ~ elev, data = class_ndvi, FUN = mean)
aggregate(diff ~ elev, data = class_ndvi, FUN = mean)
aggregate(ndvi.11 ~ elev, data = class_ndvi, FUN = mean)
#####let's do a further analysis on our change data
#this does a count of the number of cells that have a sign change (either that are positive or negative)
table(sign(lm.ndvi.elev$change))
ndvi.positive <- subset(lm.ndvi.elev, change > 0)
ndvi.negative <- subset(lm.ndvi.elev, change <= 0)
#creating output table showing stastics on change
my.summary <- function(x, na.rm=TRUE) {
result <- c(Mean = as.numeric(format(mean(x, na.rm=na.rm), digits=3)),
SD = format(sd(x, na.rm=na.rm), digits=3),
Median = format(median(x, na.rm=na.rm), digits=3),
Min = format(min(x, na.rm=na.rm), digits = 3),
Max = format(max(x, na.rm=na.rm), digits = 3),
N = length(x),
SE = sd(x) / sqrt(length(x)),
CV = abs(mean(x)) / abs(sd(x)))
}
ind3 <- sapply(ndvi.positive, is.numeric)
ind4 <- sapply(ndvi.negative, is.numeric)
#prints the two tales contrasting postitive and negative values
sapply(ndvi.positive[,ind3], my.summary)
sapply(ndvi.negative[,ind4], my.summary)
#boxplot for distribution of ndvi
ndvi.box <- boxplot(ndvi~year, data=stack, main="Distribution of NDVI values by year", ylab="NDVI")
#ANCOVA testing differences in NDVI by elevation by stream distance
######
#####
#####
# HERE!!!!!!!!!
contrasts(class_ndvi$elev)=contr.poly(3)
model.1=aov(ndvi.11 ~ stream_dist + elev, data = class_ndvi)
Anova(model.1, type = "III")
summary.lm(model.1)
posth=glht(model.1, linfct=mcp(elev="Tukey")) ##gives the post-hoc Tukey analysis of elevation, which is the factorvariable
summary(posth) ##shows the output in a nice format.
interact.model=aov(ndvi.11 ~ stream_dist + elev + stream_dist:elev, data=class_ndvi)
summary(interact.model)
#Vist these sites: http://goo.gl/yxUZ1R
# http://stats.stackexchange.com/questions/51780/how-i-make-an-ancova-in-rR
#ANCOVA elevation and stream distance on 86, 11, and change
#1986
results.86 = lm(ndvi.86 ~ stream_dist * elev, data = class_ndvi)
aov.86 <-aov(results.86)
summary(aov.86)
TukeyHSD(aov.86, "elev")
qqnorm(results.86$residuals)
plot(results.86$fitted,results$res,xlab="Fitted",ylab="Residuals")
#2011
results.11 = lm(ndvi.11 ~ stream_dist * elev , data = class_ndvi)
aov.11 <- aov(results.11)
summary(aov.11)
TukeyHSD(aov.11, "elev")
qqnorm(results.11$residuals)
head(ndvi.86)
plot(results.11$fitted,results$res,xlab="Fitted",ylab="Residuals")
#ndvi difference from endmembers
results.change = lm(diff ~ stream_dist * elev , data = class_ndvi)
aov.change <-aov(results.change)
summary(aov.change)
TukeyHSD(aov.change, "elev")
qqnorm(results.change$residuals)
plot(results.change$fitted,results$res,xlab="Fitted",ylab="Residuals")
p.stream.elev <- ggplot(class_ndvi, aes(x=stream_dist, y=ndvi.11, color=elev))+
geom_point(size=3)
##### Piecewise regression
require(segmented)
# piecewise regression on class_ndvi
#2011
x11()
plot(class_ndvi$stream_dist, class_ndvi$ndvi.11)
lm.ndvi.stream.11 <- lm(ndvi.11 ~ stream_dist, data = class_ndvi)
seg.ndvi.stream.11 <- segmented(lm.ndvi.stream.11, seg.Z = ~stream_dist, psi=100,
control = seg.control(display=TRUE))
plot(seg.ndvi.stream.11, conf.level = 0.95, shade = TRUE)
#1986
plot(class_ndvi$stream_dist, class_ndvi$ndvi.86)
lm.ndvi.stream.86 <- lm(ndvi.86 ~ stream_dist, data = class_ndvi)
seg.ndvi.stream.86 <- segmented(lm.ndvi.stream.86, seg.Z = ~stream_dist, psi=100,
control = seg.control(display=TRUE))
plot(seg.ndvi.stream.86, conf.level = 0.95, shade = TRUE)
#Estimating breakpoints
summary(seg.ndvi.stream.86)
summary(seg.ndvi.stream.11)
#identifies the changepoint as 142.4 m +/- 4.468
##RIPARIAN
#Now to classify as riparian or upland based on stream_dist threshold
class_ndvi$stream_class[class_ndvi$stream_dist <= 150] <- "Riparian"
class_ndvi$stream_class[class_ndvi$stream_dist > 150] <- "Upland"
#OK, now let's check that jam with some linear regression action
riparian.ndvi <- subset(class_ndvi, class_ndvi$stream_class == "Riparian")
upland.ndvi <- subset(class_ndvi, class_ndvi$stream_class == "Upland")
#Linear regression using the RIPARIAN data
lm.riparian.11 <- lm(ndvi.11 ~ stream_dist, data = riparian.ndvi)
lm.riparian.86 <- lm(ndvi.86 ~ stream_dist, data = riparian.ndvi)
lm.riparian.change <-lm(diff ~ stream_dist, data = riparian.ndvi)
#pearson's correlation
cor.riparian.11 <- cor(riparian.ndvi$stream_dist, riparian.ndvi$ndvi.11, use = "everything", method = "pearson")
cor.riparian.86 <- cor(riparian.ndvi$stream_dist, riparian.ndvi$ndvi.86, use = "everything", method = "pearson")
cor.riparian.change <- cor(riparian.ndvi$stream_dist, riparian.ndvi$change, use = "everything", method = "pearson")
summary(lm.riparian.86)
summary(lm.riparian.11)
summary(lm.riparian.change)
##UPLAND
lm.upland.86 <- lm(ndvi.86 ~ stream_dist, data = upland.ndvi)
lm.upland.11 <- lm(ndvi.11 ~ stream_dist, data = upland.ndvi)
lm.upland.change <- lm(change ~ stream_dist, data = upland.ndvi)
#pearson's correlation
cor.upland.11 <- cor(upland.ndvi$stream_dist, upland.ndvi$ndvi.11, use = "everything", method = "pearson")
cor.upland.86 <- cor(upland.ndvi$stream_dist, upland.ndvi$ndvi.86, use = "everything", method = "pearson")
cor.upland.change <- cor(upland.ndvi$stream_dist, upland.ndvi$change, use = "everything", method = "pearson")
summary(lm.upland.86)
summary(lm.upland.11)
summary(lm.upland.change)
plot(upland.ndvi$stream_dist, upland.ndvi$ndvi.11)
plot(riparian.ndvi$stream_dist, riparian.ndvi$ndvi.86)
# # Now we want to recombine the data to look at the mean increase in NDVI for each
# This will combing the ndvi.comp df which tells us the change in classification based on the endmembmers of 1986 and 2011 NDVI
# with the linear regression function results
class_ndvi_comp <- merge(class_ndvi, ndvi.comp, by = c("x","y"))
#CHANGE
aggregate.ndvi.comp <- aggregate(change ~ class, data = class_ndvi_comp, FUN = function(x) c( = sd(x), AVG= mean(x)))
print(aggregate.ndvi.comp)
aggregate.ndvi.elev.comp <- aggregate(change ~ class*elev, data = class_ndvi_comp, mean, na.rm=TRUE)
print(aggregate.ndvi.elev.comp)
aggregate.ndvi.elev.stream.comp <- aggregate(change ~ class*elev*stream_class, data = class_ndvi_comp, mean, na.rm=TRUE)
print(aggregate.ndvi.elev.stream.comp)
aggregate.ndvi.stream.comp <- aggregate(change ~ class*stream_class, data = class_ndvi_comp, mean, na.rm=TRUE)
print(aggregate.ndvi.stream.comp)
#NDVI
aggregate.ndvi <- aggregate(ndvi.11 ~ class, data = class_ndvi_comp, mean, na.rm=TRUE)
print(aggregate.ndvi)
aggregate.ndvi.elev <- aggregate(ndvi.11 ~ class*elev, data = class_ndvi_comp, mean, na.rm=TRUE)
print(aggregate.ndvi.elev)
aggregate.ndvi.elev.stream <- aggregate(ndvi.11 ~ class*elev*stream_class, data = class_ndvi_comp, mean, na.rm=TRUE)
print(aggregate.ndvi.elev.stream)
aggregate.ndvi.stream <- aggregate(ndvi.11 ~ class*stream_class, data = class_ndvi_comp, mean, na.rm=TRUE)
print(aggregate.ndvi.stream)
as.table(aggregate.ndvi.stream)