get_pca_predictions: Return PCA predictions.

View source: R/pca.R

get_pca_predictionsR Documentation

Return PCA predictions.


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.


  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, = TRUE,
  print.summary = getOption("itsadug_print")



A gam object, produced by gam or bam.


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


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


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.


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.


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


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


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.


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


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


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


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

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


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


Jacolien van Rij

See Also

plot_pca_surface, prcomp

Other Functions for PCA interpretation: plot_pca_surface()


# 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:
# 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'),
# 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)

## End(Not run)

itsadug documentation built on June 17, 2022, 5:05 p.m.