backPredict: Transform output from Prcomp / Princomp

Description Usage Arguments Details Value Author(s) See Also Examples

Description

These functions reverse the outcome of the predict function when the class of the first argument is either prcomp or princomp. These functions are used to transform data where the variables are principal components of a dataset of original variables, given the object.

Usage

1
2
3
backPredict(object, newdata)

backPredictVar(object, new.vardata, only.var = TRUE)

Arguments

object

Object of class inheriting from princomp or prcomp

newdata

A matrix of principal components which is to be converted back to the original output variables. An important feature here is that newdata can have a reduced number of dimensions with which to predict, however it has to be a matrix.

new.vardata

A matrix of variances where each column represents the variances of one principal component's prediction for many observations, and each row represents the variances of multiple principal components of one observation.

only.var

This parameter lets the user choose how the output of the function backPredictVar should appear. The default option is set to TRUE. In this case the output of the function only contains the variances of each observation with the output variable. If FALSE this function returns an array of 3 dimensions representing the cross covariances of output variables and observations. The 1st dimension is the number of rows in new.vardata and the 2nd and 3rd dimensions represent a variance covariance matrix between output j and output k for row i in new.vardata.

Details

backPredict defines a function to reverse the outcome of the predict.prcomp or predict.princomp function. It is used to transform data (typically values predicted using regressions) where the variables are principal components of a dataset of original variables, given in the object.

backPredictVar defines a similar function to reverse the outcome of the predict.prcomp or predict.princomp function. It is used to transform data that represents variances of principal components of an original dataset given in the object. It uses the linear variance transformation property.

Value

backPredict returns a matrix of data in terms of the original output variables.

backPredictVar returns a matrix of variances (and covariances if only.var = FALSE) of each observation with the original output variable

Author(s)

Sajni Malde

See Also

princomp, prcomp, predict.prcomp

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
library(mvtnorm)

#defining data
Sigma <- matrix(c(10.3, 3.6,3.6,2.4),2,2) 
out <- rmvnorm(n=100000, c(1.2,2.4), Sigma); colnames(out) <- c('Y1', 'Y2')
cov(out)
new.out <- rmvnorm(n=100000, c(1.2,2.4), Sigma); colnames(new.out) <- c('Y1', 'Y2')

# using the 'princomp' function to convert the data
out.pca <- princomp(out) # converting the data to principal components
# predicting principal components for a new data set
new.out.pca<- predict(out.pca, new.out) 

# applying backPredict and comparing values
backpredict <- backPredict(out.pca, new.out.pca)
head(backpredict) # should be quite similar to head(new.out)
# reducing the dimensions # only 1 components used to 'backPredict'
backpredict2 <- backPredict(out.pca, new.out.pca[, 1, drop = FALSE])

# applying backPredictVar and comparing values
# check to see if assuming off diagonals are equal to zero is a reasonable assumption
cov(new.out.pca) 
(backpredict.var <- backPredictVar(out.pca, new.vardata = matrix(diag(cov(new.out.pca)), 
                                   ncol = 2), only.var = FALSE)[1,,])
# backpredict.var should be very close to cov(out)

OakleyJ/MUCM documentation built on May 7, 2019, 9:01 p.m.