Running examples in ‘gamm4-Ex.R’ failed
The error most likely occurred in:

> ### Name: gamm4
> ### Title: Generalized Additive Mixed Models using lme4 and mgcv
> ### Aliases: gamm4
> ### Keywords: models smooth regression
> 
> ### ** Examples
> 
> ## NOTE: most examples are flagged as 'do not run' simply to
> ## save time in package checking on CRAN.
> 
> ###################################
> ## A simple additive mixed model...
> ###################################
> library(gamm4)
> 
> set.seed(0) 
> dat <- gamSim(1,n=400,scale=2) ## simulate 4 term additive truth
Gu & Wahba 4 term additive model
> ## Now add 20 level random effect `fac'...
> dat$fac <- fac <- as.factor(sample(1:20,400,replace=TRUE))
> dat$y <- dat$y + model.matrix(~fac-1)%*%rnorm(20)*.5
> 
> br <- gamm4(y~s(x0)+x1+s(x2),data=dat,random=~(1|fac))
> plot(br$gam,pages=1)
> 
> summary(br$gam) ## summary of gam

Family: gaussian 
Link function: identity 

Formula:
y ~ s(x0) + x1 + s(x2)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   4.5910     0.2741   16.75   <2e-16 ***
x1            6.3301     0.3822   16.56   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Approximate significance of smooth terms:
        edf Ref.df      F  p-value    
s(x0) 3.237  3.237  5.939 0.000445 ***
s(x2) 8.161  8.161 85.260  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

R-sq.(adj) =  0.697   
lmer.REML = 1798.2  Scale est. = 4.5333    n = 400
> summary(br$mer) ## underlying mixed model
Linear mixed model fit by REML ['lmerMod']

REML criterion at convergence: 1798.2

Scaled residuals: 
     Min       1Q   Median       3Q      Max 
-2.38017 -0.63103 -0.05911  0.67192  2.43641 

Random effects:
 Groups   Name        Variance Std.Dev.
 fac      (Intercept)   0.4983  0.7059 
 Xr.0     s(x2)       690.9163 26.2853 
 Xr       s(x0)         4.3478  2.0851 
 Residual               4.5333  2.1292 
Number of obs: 400, groups:  fac, 20; Xr.0, 8; Xr, 8

Fixed effects:
             Estimate Std. Error t value
X(Intercept)   4.5910     0.2741  16.748
Xx1            6.3301     0.3822  16.564
Xs(x0)Fx1      0.4081     0.6483   0.629
Xs(x2)Fx1      4.7592     2.2445   2.120

Correlation of Fixed Effects:
          X(Int) Xx1    X(0)F1
Xx1       -0.716              
Xs(x0)Fx1  0.037 -0.048       
Xs(x2)Fx1  0.069 -0.090 -0.015
> anova(br$gam) 

Family: gaussian 
Link function: identity 

Formula:
y ~ s(x0) + x1 + s(x2)

Parametric Terms:
   df     F p-value
x1  1 274.4  <2e-16

Approximate significance of smooth terms:
        edf Ref.df      F  p-value
s(x0) 3.237  3.237  5.939 0.000445
s(x2) 8.161  8.161 85.260  < 2e-16
> 
> ## compare gam fit of the same
> bg <- gam(y~s(x0)+x1+s(x2)+s(fac,bs="re"),
+           data=dat,method="REML")
> plot(bg,pages=1)
> gam.vcomp(bg)

Standard deviations and 0.95 confidence intervals:

           std.dev      lower      upper
s(x0)    9.7811599  2.7302776  35.040792
s(x2)  112.3162387 62.4514843 201.995799
s(fac)   0.7058762  0.4453773   1.118740
scale    2.1291599  1.9802165   2.289306

Rank: 4/4
> 
> ##########################
> ## Poisson example GAMM...
> ##########################
> ## simulate data...
> x <- runif(100)
> fac <- sample(1:20,100,replace=TRUE)
> eta <- x^2*3 + fac/20; fac <- as.factor(fac)
> y <- rpois(100,exp(eta))
> 
> ## fit model and examine it...
> bp <- gamm4(y~s(x),family=poisson,random=~(1|fac))
> plot(bp$gam)
> bp$mer
Generalized linear mixed model fit by maximum likelihood (Laplace
  Approximation) [glmerMod]
 Family: poisson  ( log )
      AIC       BIC    logLik  deviance  df.resid 
 487.6563  498.0769 -239.8281  479.6563        96 
Random effects:
 Groups Name        Std.Dev.
 fac    (Intercept) 0.2701  
 Xr     s(x)        0.5536  
Number of obs: 100, groups:  fac, 20; Xr, 8
Fixed Effects:
X(Intercept)      Xs(x)Fx1  
      1.5926        0.7677  
> 
> ## Not run: 
> ##D #################################################################
> ##D ## Add a factor to the linear predictor, to be modelled as random
> ##D ## and make response Poisson. Again compare `gamm' and `gamm4'
> ##D #################################################################
> ##D 
> ##D g <- as.factor(sample(1:20,400,replace=TRUE))
> ##D dat$f <- dat$f + model.matrix(~ g-1)%*%rnorm(20)*2
> ##D dat$y <- rpois(400,exp(dat$f/7))
> ##D 
> ##D b2<-gamm(y~s(x0)+s(x1)+s(x2)+s(x3),family=poisson,
> ##D          data=dat,random=list(g=~1))
> ##D plot(b2$gam,pages=1)
> ##D 
> ##D b2r<-gamm4(y~s(x0)+s(x1)+s(x2)+s(x3),family=poisson,
> ##D          data=dat,random = ~ (1|g))
> ##D 
> ##D plot(b2r$gam,pages=1)
> ##D 
> ##D rm(dat)
> ##D vis.gam(b2r$gam,theta=35)
> ##D 
> ##D 
> ##D ##################################
> ##D # Multivariate varying coefficient
> ##D # With crossed and nested random 
> ##D # effects.
> ##D ##################################
> ##D 
> ##D ## Start by simulating data...
> ##D 
> ##D f0 <- function(x, z, sx = 0.3, sz = 0.4) {
> ##D             (pi^sx * sz) * (1.2 * exp(-(x - 0.2)^2/sx^2 - (z -
> ##D                 0.3)^2/sz^2) + 0.8 * exp(-(x - 0.7)^2/sx^2 -
> ##D                 (z - 0.8)^2/sz^2))
> ##D         }
> ##D f1 <- function(x2) 2 * sin(pi * x2)
> ##D f2 <- function(x2) exp(2 * x2) - 3.75887
> ##D f3 <- function (x2) 0.2 * x2^11 * (10 * (1 - x2))^6 + 10 * (10 * x2)^3 *
> ##D             (1 - x2)^10
> ##D 
> ##D n <- 1000
> ##D 
> ##D ## first set up a continuous-within-group effect...
> ##D 
> ##D g <- factor(sample(1:50,n,replace=TRUE)) ## grouping factor
> ##D x <- runif(n)                       ## continuous covariate
> ##D X <- model.matrix(~g-1)
> ##D mu <- X%*%rnorm(50)*.5 + (x*X)%*%rnorm(50)
> ##D 
> ##D ## now add nested factors...
> ##D a <- factor(rep(1:20,rep(50,20)))
> ##D b <- factor(rep(rep(1:25,rep(2,25)),rep(20,50)))
> ##D Xa <- model.matrix(~a-1)
> ##D Xb <- model.matrix(~a/b-a-1)
> ##D mu <- mu + Xa%*%rnorm(20) + Xb%*%rnorm(500)*.5
> ##D 
> ##D ## finally simulate the smooth terms
> ##D v <- runif(n);w <- runif(n);z <- runif(n)
> ##D r <- runif(n)
> ##D mu <- mu + f0(v,w)*z*10 + f3(r) 
> ##D 
> ##D y <- mu + rnorm(n)*2 ## response data
> ##D 
> ##D ## First compare gamm and gamm4 on a reduced model
> ##D 
> ##D br <- gamm4(y ~ s(v,w,by=z) + s(r,k=20,bs="cr"),random = ~ (1|a/b))
> ##D 
> ##D ba <- gamm(y ~ s(v,w,by=z) + s(r,k=20,bs="cr"),random = list(a=~1,b=~1),method="REML")
> ##D 
> ##D 
> ##D par(mfrow=c(2,2))
> ##D plot(br$gam)
> ##D 
> ##D plot(ba$gam)
> ##D 
> ##D ## now fit the full model
> ##D 
> ##D br <- gamm4(y ~ s(v,w,by=z) + s(r,k=20,bs="cr"),random = ~ (x+0|g) + (1|g) + (1|a/b))
> ##D 
> ##D br$mer
> ##D br$gam
> ##D plot(br$gam)
> ##D 
> ##D ## try a Poisson example, based on the same linear predictor...
> ##D 
> ##D lp <- mu/5
> ##D y <- rpois(exp(lp),exp(lp)) ## simulated response
> ##D 
> ##D ## again compare gamm and gamm4 on reduced model
> ##D 
> ##D br <- gamm4(y ~ s(v,w,by=z) + s(r,k=20,bs="cr"),family=poisson,random = ~ (1|a/b))
> ##D 
> ##D ba <- gamm(y ~ s(v,w,by=z) + s(r,k=20,bs="cr"),family=poisson,random = list(a=~1,b=~1))
> ##D 
> ##D par(mfrow=c(2,2))
> ##D plot(br$gam)
> ##D plot(ba$gam)
> ##D 
> ##D ## and now fit full version (very slow)...
> ##D 
> ##D br <- gamm4(y ~ s(v,w,by=z) + s(r,k=20,bs="cr"),family=poisson,random = ~ (x|g) + (1|a/b))
> ##D br$mer
> ##D br$gam
> ##D plot(br$gam)
> ##D 
> ##D 
> ##D ####################################
> ##D # Different smooths of x2 depending 
> ##D # on factor `fac'...
> ##D ####################################
> ##D dat <- gamSim(4)
> ##D 
> ##D br <- gamm4(y ~ fac+s(x2,by=fac)+s(x0),data=dat)
> ##D plot(br$gam,pages=1)
> ##D summary(br$gam)
> ##D 
> ##D 
> ##D ####################################
> ##D # Timing comparison with `gam'...  #                                      
> ##D ####################################
> ##D 
> ##D dat <- gamSim(1,n=600,dist="binary",scale=.33)
> ##D 
> ##D system.time(lr.fit0 <- gam(y~s(x0)+s(x1)+s(x2),
> ##D             family=binomial,data=dat,method="ML"))
> ##D 
> ##D system.time(lr.fit <- gamm4(y~s(x0)+s(x1)+s(x2),
> ##D             family=binomial,data=dat))
> ##D 
> ##D lr.fit0;lr.fit$gam
> ##D cor(fitted(lr.fit0),fitted(lr.fit$gam))
> ##D 
> ##D ## plot model components with truth overlaid in red
> ##D op <- par(mfrow=c(2,2))
> ##D fn <- c("f0","f1","f2","f3");xn <- c("x0","x1","x2","x3")
> ##D for (k in 1:3) {
> ##D   plot(lr.fit$gam,select=k)
> ##D   ff <- dat[[fn[k]]];xx <- dat[[xn[k]]]
> ##D   ind <- sort.int(xx,index.return=TRUE)$ix
> ##D   lines(xx[ind],(ff-mean(ff))[ind]*.33,col=2)
> ##D }
> ##D par(op)
> ## End(Not run)
> 
> ######################################
> ## A "signal" regression example, in
> ## which a univariate response depends
> ## on functional predictors.
> ######################################
> 
> ## simulate data first....
> 
> rf <- function(x=seq(0,1,length=100)) {
+ ## generates random functions...
+   m <- ceiling(runif(1)*5) ## number of components
+   f <- x*0;
+   mu <- runif(m,min(x),max(x));sig <- (runif(m)+.5)*(max(x)-min(x))/10
+   for (i in 1:m) f <- f+ dnorm(x,mu[i],sig[i])
+   f
+ }
> 
> x <- seq(0,1,length=100) ## evaluation points
> 
> ## example functional predictors...
> par(mfrow=c(3,3));for (i in 1:9) plot(x,rf(x),type="l",xlab="x")
> 
> ## simulate 200 functions and store in rows of L...
> L <- matrix(NA,200,100) 
> for (i in 1:200) L[i,] <- rf()  ## simulate the functional predictors
> 
> f2 <- function(x) { ## the coefficient function
+   (0.2*x^11*(10*(1-x))^6+10*(10*x)^3*(1-x)^10)/10 
+ }
> 
> f <- f2(x) ## the true coefficient function
> 
> y <- L%*%f + rnorm(200)*20 ## simulated response data
> 
> ## Now fit the model E(y) = L%*%f(x) where f is a smooth function.
> ## The summation convention is used to evaluate smooth at each value
> ## in matrix X to get matrix F, say. Then rowSum(L*F) gives E(y).
> 
> ## create matrix of eval points for each function. Note that
> ## `smoothCon' is smart and will recognize the duplication...
> X <- matrix(x,200,100,byrow=TRUE) 
> 
> ## compare `gam' and `gamm4' this time
> 
> b <- gam(y~s(X,by=L,k=20),method="REML")
> br <- gamm4(y~s(X,by=L,k=20))
> par(mfrow=c(2,1))
> plot(b,shade=TRUE);lines(x,f,col=2)
> plot(br$gam,shade=TRUE);lines(x,f,col=2)
> 
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> ### * <FOOTER>
> ###
> options(digits = 7L)
> base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n")
Time elapsed:  7.981 0.347 9.078 0 0 
> grDevices::dev.off()

 *** caught segfault ***
address 93, cause 'memory not mapped'

Traceback:
 1: grDevices::dev.off()
An irrecoverable exception occurred. R is aborting now ...