1

I'm compiling using RMarkdown and knitr a short protocol for me (in one html file), about modelling. The basis is Zuur, A. F. "leno, E. N, Walker, NJ, Saveliev, AA & Smith, G M. 2009: Mixed effects models and extensions in ecology with R."

I downloaded the code and the datasets shared in their website1 and I'm mixing it with other sources and comments to produce something useful for me. In this website you can freely download the two dataset I'm using.

The main problem is that I'm trying to mix something made with mgcv package and something with gam package.

I clearly understood from this two topics that this is the problem:

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

But I would like to find a solution. Obviously as answered in theese two topics specify the package or detach the unused one does not work. I just tried in my code.

Those are the parts that are causing me problems:

---
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)

```



Does anyone knows a way to "detach" completely mgcv or gam? Or a code that can "reaload" the entire environment when I have to compile the gam part?

My sessionInfo if it is useful.

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
4

0 回答 0