我想检查我的数据中的空间自相关性。因此,我正在尝试绘制几个GAM模型的残差的变异函数,但我在网络上找到的代码对我没有帮助,或者我只是无法使用它们。
我知道如何为我的原始数据绘制变异函数,但我不能为我的模型数据(残差)做它。
下面是我的代码:
library(readr)
library(mgcv)
library(ggplot2)
library(tidyverse)
dat<-read_csv("dat.csv")
View(dat)
unique(dat$parameter)
dats <- filter(dat, parameter == "esd")
model1 <- gam(value ~ s(lat, long) + s(year, k=5), data=dats)
model2 <- gam(value ~ s(lat) + s(long) + s(year, k=5), data=dats)
model3 <- gam(value ~ s(lat) + s(long) , data=dats)
model4 <- gam(value ~ s(lat) + s(year, k=5), data=dats)
model5 <- gam(value ~ s(long) + s(year, k=5), data=dats)
AIC(model1, model2, model3, model4, model5)
summary(model1)
plot(model1, pages = 1)
### delete missing values
sum(is.na(dats))
dats2 <- na.omit(dats)
sum(is.na(dats2))
unique(dats2)
# A tibble: 249 x 8
station parameter value year temp sal lat long
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 BB0001 esd 7726. 2010 18.5 6.43 55.3 14.4
2 BB0002 esd 11338. 2010 18.3 6.55 55.4 14.5
3 BB0003 esd 11860. 2010 18.2 6.46 55.4 14.6
4 BB0004 esd 16961. 2010 17.9 6.37 55.4 15.1
5 BB0005 esd 11400. 2010 18.4 6.38 55.4 15.3
6 BB0006 esd 19815. 2010 18.7 6.48 55.4 15.4
7 BB0007 esd 8823. 2010 18.5 5.52 55.4 16.0
8 BB0008 esd 7761. 2010 18.2 6.25 55.5 15.6
9 BB0009 esd 3216. 2010 18.3 6.21 55.5 16.2
10 BB0010 esd 5720. 2010 18.1 6.14 55.4 16.2
# ... with 239 more rows
var.dat <- variogram(value~1, loc= ~lat+long, data=dats2)
plot(var.dat)
## works fine
**plot(variogram(model1$residuals, robust = TRUE, data = dats2, form = ~lat))**所以最后一部分就是我的问题所在。感谢您的任何帮助!
发布于 2019-05-13 20:52:08
过了一段时间,我自己想明白了。
答案很简单。我必须先从我的模型中提取残差,然后再绘制它们:
dats$resid <- residuals(model1)
var.dat_resid <- variogram(resid~1, loc= ~lat+long, data=dats)不管怎样,谢谢你!
https://stackoverflow.com/questions/56097905
复制相似问题