如何解决mgcv ang gam包的兼容性问题?

时间:2016-03-29 15:36:01

标签: r gam mgcv

我正在使用RMarkdown和knitr编译一个简短的协议(在一个html文件中),关于建模。基础是Zuur,A.F。" leno,E.N,Walker,NJ,Saveliev,AA& Smith,G M. 2009:R。"

中的混合效应模型和生态学扩展

我下载了他们网站1中共享的代码和数据集,并且我将其与其他来源和评论混合以生成对我有用的内容。在本网站中,您可以免费下载我使用的两个数据集。

主要问题是我试图将使用mgcv包装制作的东西与使用gam包装的东西混合。

我从这两个主题中清楚地了解到这就是问题所在:

R Package conflict between gam and mgcv?
Are there known compatibility issues with R package mgcv? Are there general rules for compatibility?

但我想找到解决方案。显然,正如在theese中回答的两个主题指定包或分离未使用的一个不起作用。我刚试过我的代码。

这些是导致我出现问题的部分:

---
title: "error"
author: "Simone Marini"
date: "29 marzo 2016"
output: html_document
---

```{r setup, include = FALSE, cache = FALSE}  
knitr::opts_chunk$set(error = TRUE)  # to allow rendering to html even if there are errors
```

```{r}
Loyn <- read.table(file = "zuur_data/Loyn.txt", header = TRUE, dec = ".")
Loyn$fGRAZE <- factor(Loyn$GRAZE) # Transform in factor Graze data (from 1 to 5 -> 5 classes)
Loyn$L.AREA<-log10(Loyn$AREA)
Loyn$L.DIST<-log10(Loyn$DIST)
Loyn$L.LDIST<-log10(Loyn$LDIST)
```

```{r}
library(mgcv)
AM1<-mgcv::gam(ABUND~s(L.AREA)+s(L.DIST)+s(L.LDIST)+
s(YR.ISOL)+s(ALT)+fGRAZE, data = Loyn)

# The anova command does not apply a sequential F-test as it did for the linear regression model.
# Instead, it gives the Wald test (approximate!) that shows the significance of each term in the model.

anova(AM1)

AM2<-mgcv:::gam(ABUND ~ s(L.AREA, bs = "cs") + s(L.DIST, bs = "cs") +
             s(L.LDIST,bs = "cs") + s(YR.ISOL, bs = "cs") +
             s(ALT, bs = "cs") + fGRAZE, data = Loyn)
anova(AM2)

AM3 <- mgcv:::gam(ABUND ~ s(L.AREA, bs = "cs") + fGRAZE, data = Loyn)

#Model plot

plot(AM3)

E.AM3 <- resid(AM3) # Residuals
Fit.AM3 <- fitted(AM3) # Fitted values
plot(x = Fit.AM3, y = E.AM3, xlab = "Fitted values",
   ylab = "Residuals") # Graph

M3<-lm(ABUND ~ L.AREA + fGRAZE, data = Loyn)
AM3<-mgcv:::gam(ABUND ~ s(L.AREA, bs = "cs") + fGRAZE, data = Loyn)
anova(M3, AM3)
```

```{r}
rm(list=ls())
detach("package:mgcv")
ISIT <- read.table(file = "zuur_data/ISIT.txt", header = TRUE, dec = ".")
ISIT$fStation<-factor(ISIT$Station)

op <- par(mfrow=c(2,2),mar=c(5,4,1,2))
Sources16<-ISIT$Sources[ISIT$Station==16]
Depth16<-ISIT$SampleDepth[ISIT$Station==16]
plot(Depth16,Sources16,type="p")

library(gam)
M2<-gam:::gam(Sources16~gam::lo(Depth16,span=0.5))
plot(M2,se=T)

P2 <- predict(M2, se = TRUE)
plot(Depth16, Sources16, type = "p")
I1 <- order(Depth16)
lines(Depth16[I1], P2$fit[I1], lty = 1)
lines(Depth16[I1], P2$fit[I1] + 2 * P2$se[I1], lty = 2)
lines(Depth16[I1], P2$fit[I1] - 2 * P2$se[I1], lty = 2)

par(op)

```



有没有人知道如何分离&#34;完全mgcv或gam?或者是一个可以&#34; reaload&#34;我必须编译gam部分时的整个环境?

我的sessionInfo是否有用。

R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252    LC_MONETARY=Italian_Italy.1252
[4] LC_NUMERIC=C                   LC_TIME=Italian_Italy.1252    

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lattice_0.20-33 gam_1.12        foreach_1.4.3   mgcv_1.8-11     nlme_3.1-124   

loaded via a namespace (and not attached):
 [1] Matrix_1.2-3     htmltools_0.3    tools_3.2.3      yaml_2.1.13      codetools_0.2-14 rmarkdown_0.9.2 
 [7] grid_3.2.3       iterators_1.0.8  knitr_1.12.3     digest_0.6.9

0 个答案:

没有答案