我试图使用GLS模型平均值手动预测(因为预测命令不起作用)。 我能够从完整(单一)模型中预测,但不幸的是平均值出现问题,特别是在标准误差计算上。 你知道代码有什么问题吗?
我在这里提供了有关错误和数据集下方注释的代码:
library(nlme)
library(MuMIn)
options(na.action=na.fail)
# Full model
rs_sin <- gls(Snlpall ~ marg + urban + crop + npatches + siepi + guyot,
correlation=corGaus(form=~x_centroide+y_centroide),data=dati1, method="ML")
# Averaged model
dredge_rs_sin <- dredge(rs_sin,rank="AICc")
avg_rs_sin <- model.avg(get.models(dredge_rs_sin, subset=delta<2))
#Newdata
MyData <- data.frame(
Intercept=1,
marg=mean(dati1$marg,na.rm=TRUE),
urban=mean(dati1$urban,na.rm=TRUE),
crop=mean(dati1$crop,na.rm=TRUE),
npatches= seq(3,59,length=50),
siepi=mean(dati1$siepi,na.rm=TRUE),
guyot=mean(dati1$guyot,na.rm=TRUE))
X <- model.matrix (~npatches+crop+guyot+marg+urban+siepi,data=MyData)
#Plot for full model -> it works
Betas <- coef(rs_sin)
FitManual <- Betas[1] + X %*% Betas
SE <- sqrt(diag((Betas[1] +X) %*% vcov(rs_sin)%*% t(X)))
plot(x=dati1$npatches,y=dati1$Snlpall,pch=16,
xlab="",
ylab="R")
lines(x=MyData$npatches,y=FitManual)
lines(x=MyData$npatches,y=FitManual+1.96*SE,lty=3)
lines(x=MyData$npatches,y=FitManual-1.96*SE,lty=3)
# Plot for averaged model --> doesn't work
Betas1 <- coef(avg_rs_sin)
FitManual1 <- Betas[1] + X %*% Betas
SE1 <- sqrt(diag((Betas[1] +X) %*% vcov(avg_rs_sin)%*% t(X))) # think there is something wrong here with the vcov matrix.
plot(x=dati1$npatches,y=dati1$Snlpall,pch=16,
xlab="npatches",
ylab="R")
lines(x=MyData$npatches,y=FitManual1)
lines(x=MyData$npatches,y=FitManual1+1.96*SE1,lty=3)
lines(x=MyData$npathes,y=FitManual1-1.96*SE1,lty=3)
dati1 <- "Snlpall x_centroide y_centroide crop marg urban npatches guyot siepi
8 650582,173 5065311,29 5,769653689 9,795154729 0 28 1,960020934 0
17 650780,186 5079478,53 25,19722986 13,71890437 0 28 0 302,69
14 650837,792 5080986,29 26,34250299 9,567729568 3,046477232 37 96,3649795 837,52
12 650219,122 5080711,8 8,395660557 24,13350622 0 52 6,545660511 509,33
13 651557,988 5101389,96 7,292986941 10,47537282 0,588274573 18 86,98728834 188,05
14 655830,737 5084020,78 0 25,01851151 5,185242251 27 37,06489205 1131,77
13 656885,037 5085527,09 6,239801769 15,938253 2,089378711 32 6,732789886 217,6
10 656439,59 5086246,3 1,566067089 28,25899447 2,414442568 31 20,31262478 652,75
15 657651,392 5088343,61 0 26,92627763 0 10 0 110,04
15 665088,801 5106474,71 2,344176929 9,080539984 20,04834903 16 50,55249007 295,9
19 666923,149 5105672,06 3,550300232 6,603493481 13,4842657 33 1,713945878 610,21
14 666330,463 5105218,78 8,814677781 19,04378682 2,238734061 19 22,09936244 461,42
8 663247,545 5089075,69 0 14,74065481 0 29 0 67,59
15 649285,276 5091888,15 2,044514273 16,78475343 0,56687594 29 0 231,29
21 647238,32 5086540,65 5,952167331 19,49417274 2,915539422 27 14,99881993 778,98
16 644288,69 5084165,78 1,047482846 8,869228822 5,744886625 25 0 288,87
16 643016,618 5086852,26 0 18,82747991 0,715776494 50 19,76403792 130,19
16 651591,549 5065387,62 0 19,10503177 9,14077972 29 13,88805625 955,05
17 651623,279 5067082,62 0 21,15942749 7,353140001 32 14,18706054 577,39
11 644101,088 5085295,24 2,112922262 8,341540175 10,77505968 20 7,039046512 143,35
13 656501,846 5104763,24 15,79404027 6,589650793 0 26 100 202,18
14 653489,915 5104621,42 0,427149926 8,334455888 0,981809052 35 17,56994026 401,91
10 652847,844 5101659,97 2,235819659 20,96245805 0 22 0 507,62
2 651140,976 5100582,37 0 8,49665102 0 3 94,38359412 0
14 666901,441 5123370,88 0 10,84161287 0 21 0 10,82
14 665505,438 5122295,29 2,668232966 23,09955977 0 23 0 98,18
9 661432,335 5120828,54 5,832749208 3,193257302 0,113399036 19 0 0
16 656108,474 5071049,08 0 10,05094278 0 26 0 68,57
16 665694,213 5118017,03 0 11,64461482 1,350252049 31 4,662488133 267,36
7 664802,83 5120137,97 0 8,040119106 0 15 0,31406122 0
8 663145,929 5118602,28 0 8,20656824 0 23 0,651391704 0
8 663806,037 5115087,15 0 19,83582055 0,348439127 22 12,18343538 384,98
14 662936,661 5113098,95 1,130733095 12,00008334 12,16962516 17 29,22692277 169,22
14 662385,647 5112502,6 0,96564664 15,34754887 5,447093834 24 0 687,09
12 664353,985 5110112,23 0 16,33680027 7,325945347 29 12,12448428 771,94
15 664489,723 5109450,36 9,026212203 14,25774618 1,005842738 28 8,199504891 622,77
21 666008,201 5114424,61 4,776382501 19,46280561 0 46 0 321,08
16 668731,333 5113994,17 1,752648429 13,46189499 2,525695809 59 9,012836502 105,69
12 673669,191 5117163,02 1,280555242 15,88511352 3,416913024 46 65,87803482 356,32
10 672124,789 5115978,86 0 11,89245635 0 50 0,466710926 75,24
9 670545,121 5114535,87 0 16,6675973 1,591922283 14 97,85829241 226,43
11 655410,906 5076167,76 1,587319162 3,948184091 0 25 0 9,02
8 658237,835 5087085,83 6,122725978 8,03795217 0 29 0 5,21
12 656026,012 5077108,7 0,304283198 13,23194576 2,342847094 30 1,443417751 488,66
6 661668,979 5087813,09 0 6,777864995 0 33 10,35774909 29,03
18 654752,781 5079706,23 0 17,852161 1,910107105 34 0,15005518 214,79
15 652009,008 5078389,62 1,5591955 17,99956839 0 32 6,782792698 433,66"
非常感谢!
答案 0 :(得分:0)
根据Ben Bolker的建议,我删除了一个预测器(siepi)和Betas1。 这似乎纠正了vcov矩阵上的问题,但正如你所看到的那样,拦截似乎存在问题。 这对我来说非常奇怪!
library(nlme)
library(MuMIn)
options(na.action=na.fail)
# Full model
rs_sin <- gls(Snlpall ~ marg + urban + crop + npatches + guyot,
correlation=corGaus(form=~x_centroide+y_centroide),data=dati1, method="ML")
# Averaged model
dredge_rs_sin <- dredge(rs_sin,rank="AICc")
avg_rs_sin <- model.avg(get.models(dredge_rs_sin, subset=delta<2))
#Newdata
MyData <- data.frame(
Intercept=1,
marg=mean(dati1$marg,na.rm=TRUE),
urban=mean(dati1$urban,na.rm=TRUE),
crop=mean(dati1$crop,na.rm=TRUE),
npatches= seq(3,59,length=50),
guyot=mean(dati1$guyot,na.rm=TRUE))
X <- model.matrix (~npatches+crop+guyot+marg+urban,data=MyData)
#Plot for full model
Betas <- coef(rs_sin)
FitManual <- X %*% Betas
SE <- sqrt(diag((X) %*% vcov(rs_sin)%*% t(X)))
plot(x=dati1$npatches,y=dati1$Snlpall,pch=16,
xlab="",
ylab="R")
lines(x=MyData$npatches,y=FitManual)
lines(x=MyData$npatches,y=FitManual+1.96*SE,lty=3)
lines(x=MyData$npatches,y=FitManual-1.96*SE,lty=3)
# Plot for averaged model
Betas1 <- coef(avg_rs_sin)
FitManual1 <- X %*% Betas
SE1 <- sqrt(diag((X) %*% vcov(avg_rs_sin)%*% t(X)))
plot(x=dati1$npatches,y=dati1$Snlpall,pch=16,
xlab="npatches",
ylab="R")
lines(x=MyData$npatches,y=FitManual1)
lines(x=MyData$npatches,y=FitManual1+1.96*SE1,lty=3)
lines(x=MyData$npathes,y=FitManual1-1.96*SE1,lty=3)