Last active
October 29, 2015 13:29
-
-
Save dill/b3b67a592e762438b232 to your computer and use it in GitHub Desktop.
Make GAM term plots that extend outside the range of the data to show how bad extrapolation is for Laura M
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("best_model.Rdata") | |
library(dsm) | |
library(raster) | |
# lazily get the plot data for the rug plot | |
plotdat <- plot(M) | |
# load the raster and mudge it into the format I want | |
dists <- stack("NA_Shore_Dist_10km_mean_10km.img") | |
dists <- as.data.frame(dists) | |
dists <- dists$NA_Shore_Dist_10km_mean_10km | |
dists <- dists[!is.na(dists)] | |
# make a data frame that has the max to min value of dist to shore | |
dat <- data.frame(DistToShore = seq(min(dists), max(dists), len=1000), | |
area_km2 = 10000*10000) | |
## make our cool plot | |
# predict extrapolated values | |
extr_term <- predict(M ,dat, type="terms", se=TRUE) | |
# make predictions for the data "inside" | |
inside_dat <- dat | |
inside_dat <- inside_dat[inside_dat$DistToShore > 0 & | |
inside_dat$DistToShore < 6e5,] | |
inside_term <- predict(M, inside_dat, type="terms", se=TRUE) | |
# plot the "bad" extrapolation in red | |
plot(dat$DistToShore, extr_term$fit, type="l", col="red", | |
xlab="DistToShore", ylab="s(DistToShore)") | |
# overlay in green the "good" bit where the data is | |
lines(inside_dat$DistToShore, inside_term$fit, col="green") | |
# what about the standard errors? | |
lines(dat$DistToShore, extr_term$fit+2*extr_term$se.fit, lty=2) | |
lines(dat$DistToShore, extr_term$fit-2*extr_term$se.fit, lty=2) | |
rug(plotdat[[1]]$raw) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment