mgcv: Mixed GAM Computation Vehicle with GCV/AIC/REML Smoothness Estimation


> library(mgcv)
Loading required package: nlme

Attaching package: 'nlme'
The following object is masked from 'package:raster':

The following object is masked from 'package:HistData':

The following objects are masked from 'package:unmarked':

    getData, ranef
This is mgcv 1.8-11. For overview type 'help("mgcv-package")'.

バージョン: 1.8.11

関数名 概略
Predict.matrix Prediction methods for smooth terms in a GAM Predict matrix method functions Prediction matrix for soap film smooth
Rrank Find rank of upper triangular matrix
Tweedie GAM Tweedie families
anova.gam Approximate hypothesis tests related to GAM fits
bam Generalized additive models for very large datasets
bam.update Update a strictly additive bam model for new data.
betar GAM beta regression family
cSplineDes Evaluate cyclic B spline basis
choose.k Basis dimension choice for smooths
columb Reduced version of Columbus OH crime data
concurvity GAM concurvity measures Additive Cox Proportional Hazard Model
exclude.too.far Exclude prediction grid points too far from data
extract.lme.cov Extract the data covariance matrix from an lme object
family.mgcv Distribution families in mgcv Modify families for use in GAM fitting and checking
fixDependence Detect linear dependencies of one matrix on another
formXtViX Form component of GAMM covariance matrix
formula.gam GAM formula
fs.test FELSPLINE test function
full.score GCV/UBRE score for use within nlm
gam Generalized additive models with integrated smoothness estimation
gam.check Some diagnostics for a fitted gam model
gam.control Setting GAM fitting defaults
gam.convergence GAM convergence and performance issues GAM P-IRLS estimation with GCV/UBRE smoothness estimation
gam.fit3 P-IRLS GAM estimation with GCV & UBRE/AIC or RE/ML derivative calculation
gam.models Specifying generalized additive models
gam.outer Minimize GCV or UBRE score of a GAM using 'outer' iteration
gam.scale Scale parameter estimation in GAMs
gam.selection Generalized Additive Model Selection
gam.side Identifiability side conditions for a GAM
gam.vcomp Report gam smoothness estimates as variance components
gam2objective Objective functions for GAM smoothing parameter estimation
gamObject Fitted gam object
gamSim Simulate example data for GAMs
gamm Generalized Additive Mixed Models
gaulss Gaussian location-scale model family
get.var Get named variable or evaluate expression from list or data.frame
in.out Which of a set of points lie within a polygon defined region
inSide Are points inside boundary?
influence.gam Extract the diagonal of the influence/hat matrix for a GAM
initial.sp Starting values for multiple smoothing parameter estimation
interpret.gam Interpret a GAM formula
jaga m Just Another Gibbs Additive Modeller: JAGS support for mgcv.
ldTweedie Log Tweedie density evaluation
linear.functional.terms Linear functionals of a smooth in GAMs
logLik.gam Log likelihood for a fitted GAM, for AIC
ls.size Size of list elements
magic Stable Multiple Smoothing Parameter Estimation by GCV or UBRE Auxilliary information from magic fit
mgcv.FAQ Frequently Asked Questions for package mgcv
mgcv.package Mixed GAM Computation Vehicle with GCV/AIC/REML
smoothness estimation and GAMMs by REML/PQL
mgcv.parallel Parallel computation in mgcv.
model.matrix.gam Extract model matrix from GAM fit
mono.con Monotonicity constraints for a cubic regression spline
mroot Smallest square root of matrix
mvn Multivariate normal additive models
negbin GAM negative binomial families Obtain a name for a new variable that is not already in use
notExp Functions for better-than-log positive parameterization
notExp2 Alternative to log parameterization for variance components The basis of the space of un-penalized functions for a TPRS
ocat GAM ordered categorical family
pcls Penalized Constrained Least Squares Fitting
pdIdnot Overflow proof pdMat class for multiples of the identity matrix
pdTens Functions implementing a pdMat class for tensor product smooths
pen.edf Extract the effective degrees of freedom associated with each penalty in a gam fit
place.knots Automatically place a set of knots evenly through covariate values
plot.gam Default GAM plotting
polys.plot Plot geographic regions defined as polygons
predict.bam Prediction from fitted Big Additive Model model
predict.gam Prediction from fitted GAM model
print.gam Print a Generalized Additive Model object.
qq.gam QQ plots for gam model residuals
rTweedie Generate Tweedie random deviates
random.effects Random effects in GAMs
residuals.gam Generalized Additive Model residuals
rig Generate inverse Gaussian random deviates
rmvn Generate multivariate normal deviates
s Defining smooths in GAM formulae
scat GAM scaled t family for heavy tailed data
single.index Single index models with mgcv
slanczos Compute truncated eigen decomposition of a symmetric matrix
smooth.construct Constructor functions for smooth terms in a GAM Adaptive smooths in GAMs Penalized Cubic regression splines in GAMs
smooth.construct.ds.smooth.spec Low rank Duchon 1977 splines
smooth.construct.fs.smooth.spec Factor smooth interactions in GAMs
smooth.construct.mrf.smooth.spec Markov Random Field Smooths P-splines in GAMs Simple random effects in GAMs Soap film smoother constructer
smooth.construct.sos.smooth.spec Splines on the sphere
smooth.construct.t2.smooth.spec Tensor product smoothing constructor
smooth.construct.tensor.smooth.spec Tensor product smoothing constructor Penalized thin plate regression splines in GAMs
smooth.terms Smooth terms in GAM
smoothCon Prediction/Construction wrapper functions for GAM smooth terms
sp.vcov Extract smoothing parameter estimator
covariance matrix from (RE)ML GAM fit
spasm.construct Experimental sparse smoothers
step.gam Alternatives to step.gam
summary.gam Summary for a GAM fit
t2 Define alternative tensor product smooths in GAM formulae
te Define tensor product smooths or tensor product interactions in GAM formulae Utility functions for constructing tensor product smooths
uniquecombs find the unique rows in a matrix
vcov.gam Extract parameter (estimator) covariance matrix from GAM fit
vis.gam Visualization of GAM objects
ziP GAM zero-inflated Poisson regression family
ziplss Zero inflated Poisson location-scale model family


> set.seed(71)
> dat <- gamSim(1,n = 25000, dist = "normal", scale = 20)
Gu & Wahba 4 term additive model
> bs <- "cr"
> k <- 12
> b <- bam(y ~ s(x0, bs = bs) + s(x1, bs = bs) + s(x2, bs = bs, k = k) + s(x3, bs = bs), data = dat)
> summary(b)

Family: gaussian 
Link function: identity 

y ~ s(x0, bs = bs) + s(x1, bs = bs) + s(x2, bs = bs, k = k) + 
    s(x3, bs = bs)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   7.8886     0.1258   62.69   <2e-16

Approximate significance of smooth terms:
        edf Ref.df      F    p-value
s(x0) 3.337  4.133  7.937 0.00000173
s(x1) 2.997  3.723 64.488    < 2e-16
s(x2) 8.528  9.830 49.003    < 2e-16
s(x3) 1.001  1.002  2.164      0.141

R-sq.(adj) =  0.0288   Deviance explained = 2.95%
fREML = 1.1024e+05  Scale est. = 395.61    n = 25000
> # plot(b,pages = 1, rug = FALSE)
> # plot(b, pages = 1, rug = FALSE, seWithMean = TRUE)



> set.seed(71)
> dat <- gamSim(1, n = 400, dist = "normal", scale = 2)
Gu & Wahba 4 term additive model
> b <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat)
> summary(b)

Family: gaussian 
Link function: identity 

y ~ s(x0) + s(x1) + s(x2) + s(x3)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   8.0896     0.1024   79.04   <2e-16

Approximate significance of smooth terms:
        edf Ref.df      F   p-value
s(x0) 2.809  3.484  7.614 0.0000275
s(x1) 4.147  5.109 59.572   < 2e-16
s(x2) 8.292  8.862 87.113   < 2e-16
s(x3) 1.000  1.000  0.045     0.833

R-sq.(adj) =  0.739   Deviance explained =   75%
GCV = 4.3792  Scale est. = 4.1904    n = 400



> # 模擬データの作成
> set.seed(71)
> dat <- gamSim(1, n = 200, scale = 2)
Gu & Wahba 4 term additive model
> b <- gamm(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat)
> # plot(b$gam, pages = 1)




  • eg
  • n
  • dist
  • scale
  • verbose
> dat <- gamSim(eg = 1, n = 400, dist = "normal", scale = 2, verbose = TRUE)
Gu & Wahba 4 term additive model
> dat %>% {
+   class(.) %>% print()
+   dplyr::glimpse(.)
+ }
[1] "data.frame"
Observations: 400
Variables: 10
$ y  (dbl) 17.554394, 6.939046, 5.624183, 5.475619, 4.632405, 9.826478...
$ x0 (dbl) 0.95100575, 0.95934235, 0.48664203, 0.10584845, 0.84856293,...
$ x1 (dbl) 0.98435759, 0.68927643, 0.19723711, 0.44315047, 0.01160870,...
$ x2 (dbl) 0.2123473, 0.4258419, 0.7573317, 0.6468084, 0.9267621, 0.39...
$ x3 (dbl) 0.687140832, 0.151121247, 0.372815891, 0.669982044, 0.00448...
$ f  (dbl) 16.270092, 7.828756, 5.404719, 6.379038, 1.952875, 10.79401...
$ f0 (dbl) 0.30662586, 0.25476549, 1.99823917, 0.65287611, 0.91601697,...
$ f1 (dbl) 7.161469, 3.969154, 1.483604, 2.426139, 1.023489, 5.804703,...
$ f2 (dbl) 8.8019967445, 3.6048370549, 1.9228759380, 3.3000230948, 0.0...
$ f3 (dbl) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...



> list(M     = matrix(runif(100), 10, 10),
+      quote = "The world is ruled by idiots because only an idiot would want to rule the world.",
+        fam = binomial()) %>% ls.size()
     M  quote    fam 
  1000    216 133800



> s(..., k=-1,fx=FALSE,bs="tp",m=NA,by=NA,xt=NULL,id=NULL,sp=NULL)