Learn R Programming

itsadug (version 2.4.1)

get_pca_predictions: Return PCA predictions.

Description

Produces perspective or contour plot views of gam model predictions of the additive effects interactions. The code is based on the script for vis.gam, but allows to cancel random effects.

Usage

get_pca_predictions(
  x,
  pca.term = NULL,
  weights = NULL,
  view = NULL,
  cond = list(),
  select = NULL,
  n.grid = 30,
  se = 1.96,
  xlim = NULL,
  ylim = NULL,
  partial = TRUE,
  rm.ranef = NULL,
  as.data.frame = TRUE,
  print.summary = getOption("itsadug_print")
)

Arguments

x

A gam object, produced by gam or bam.

pca.term

Text string, name of model predictor that represents a principle component.

weights

Named list with the predictors that are combined in the PC and their weights. See examples.

view

A two-value vector containing the names of the two terms to plot. The two terms should be part of the PC. Note that variables coerced to factors in the model formula won't work as view variables.

cond

A named list of the values to use for the other predictor terms (not in view). Used for choosing between smooths that share the same view predictors.

select

A number, selecting a single model term for printing. e.g. if you want the plot for the second smooth term set select=2.

n.grid

The number of grid nodes in each direction used for calculating the plotted surface.

se

If less than or equal to zero then only the predicted surface is plotted, but if greater than zero, then 3 surfaces are plotted, one at the predicted values minus se standard errors, one at the predicted values and one at the predicted values plus se standard errors.

xlim

A two item array giving the lower and upper limits for the x- axis scale. NULL to choose automatically.

ylim

A two item array giving the lower and upper limits for the y- axis scale. NULL to choose automatically.

partial

Logical value: whether or not to plot the partial effect (TRUE) or the summed effect (FALSE, default).

rm.ranef

Logical: whether or not to remove random effects. Default is TRUE.

as.data.frame

Logical: whether the output is returned as data frame (TRUE, default) or as list (FALSE).

print.summary

Logical: whether or not to print a summary. Default set to the print info messages option (see infoMessages).

See Also

plot_pca_surface, prcomp

Other Functions for PCA interpretation: plot_pca_surface()

Examples

Run this code
# NOT RUN {
data(simdat)
# add hypothetical correlated term:
simdat$predictor <-  (simdat$Trial+10)^.75 + rnorm(nrow(simdat))
# principal components analysis:
pca <- prcomp(simdat[, c('Trial', 'predictor')])
# only first PC term contributes:
summary(pca)
# get rotation (weights of predictors in PC):
pcar <- pca$rotation
# add PC1 to data:
simdat$PC1 <- pca$x[,1]

# }
# NOT RUN {
# model:
m1 <- bam(Y ~ Group + te(Time, PC1, by=Group) 
    + s(Time, Subject, bs='fs', m=1, k=5), data=simdat)
# inspect surface:
fvisgam(m1, view=c('Time', 'PC1'), cond=list(Group='Children'),
    rm.ranef=TRUE)
# how does Trial contribute?
p <- get_pca_predictions(m1, pca.term='PC1', weights=pcar[,'PC1'], 
    view=c('Time', 'Trial'), cond=list(Group='Children'),
    rm.ranef=TRUE, partial=FALSE)
# Note that the range of Trial is estimated based on the values of PC1.
# A better solution is to specify the range:
p <- get_pca_predictions(m1, pca.term='PC1', weights=pcar[,'PC1'], 
    view=list(Time=range(simdat$Time), Trial=range(simdat$Trial)), 
    cond=list(Group='Children'),rm.ranef=TRUE, partial=FALSE)
# plotting of the surface:
plot_pca_surface(m1, pca.term='PC1', weights=pcar[,'PC1'], 
    view=c('Time', 'Trial'), cond=list(Group='Children'),rm.ranef=TRUE)
# }

Run the code above in your browser using DataLab