Skip to content

Partial Least Squares

Dmitry Grapov edited this page May 5, 2013 · 1 revision

Here is an example for doing PLS in R.

#use PLS to find best projection explainning some factor (y)
library(pls)

# I will use an example data set from R
data(mtcars)
data<-mtcars

#scale and center (always a good idea)
scaled.data<-scale(data,center=T,scale=T)

#object to model
fct<-as.factor(data$gear) #some factor to use for plotting groups
y<-as.numeric(as.character(unlist(fct))) # convert to numeric y
paste(sum(is.na(scaled.data)), "missing values in the data")# make sure there are no missing values

#do PLS
pls.mod<-plsr(y~.,data=as.data.frame(scaled.data),ncomp=2) # this a pls model of y on all the data (scaled) and only 2 components
summary(pls.mod) # basic summary
plot(pls.mod) # plot predictions
str(pls.mod) # results from modeling

#plot scores
obj<-pls.mod$scores
plot(obj[,1],obj[,2],pch=21,bg=rainbow(nlevels(fct))[fct], xlab="LV 1",ylab="LV 2")
legend("topleft",levels(fct),fill=rainbow(nlevels(fct))) # add legend