# How to do Linear Regression in R Using Partial Least Squared Regression¶

In [2]:
# load data longley data for Econometrics
library(pls)
data(longley)
Data <- as.matrix(longley)

dim(Data)

x <- Data[,1:6]
y <- Data[,7]

# ----------------------------------------
# Using Partial Least Squared Regression
# ----------------------------------------

# fit model
fit <- plsr(Employed~., data=longley, validation="CV")

# summarize the fit
summary(fit)

# make predictions
predictions <- predict(fit, longley, ncomp=6)

# summarize accuracy
mse <- mean((longley$Employed - predictions)^2) print(mse) # plot plot(longley$Employed, predictions)

1. 16
2. 7
GNP.deflatorGNPUnemployedArmed.ForcesPopulationYearEmployed
194783.0 234.289235.6 159.0 107.6081947 60.323
194888.5 259.426232.5 145.6 108.6321948 61.122
194988.2 258.054368.2 161.6 109.7731949 60.171
195089.5 284.599335.1 165.0 110.9291950 61.187
195196.2 328.975209.9 309.9 112.0751951 63.221
195298.1 346.999193.2 359.4 113.2701952 63.639
Data: 	X dimension: 16 6
Y dimension: 16 1
Fit method: kernelpls
Number of components considered: 6

VALIDATION: RMSEP
Cross-validated using 10 random segments.
(Intercept)  1 comps  2 comps  3 comps  4 comps  5 comps  6 comps
CV           3.627    1.455    1.040   0.4751   0.5486   0.5269   0.4641
adjCV        3.627    1.420    1.037   0.4713   0.5382   0.5166   0.4507

TRAINING: % variance explained
1 comps  2 comps  3 comps  4 comps  5 comps  6 comps
X           63.88    93.35    99.99   100.00   100.00   100.00
Employed    87.91    93.70    98.51    98.65    99.16    99.55
[1] 0.0522765

In [ ]: