我正在学习主成分回归,但我不了解从PCR方法获得的结果。我使用PCR的目的是减少预测变量的数量。
例如:
library(caret)
# Load the data
data("Boston", package = "MASS")
# Split the data into training and test set
set.seed(123)
training.samples <- Boston$medv %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- Boston[training.samples, ]
test.data <- Boston[-training.samples, ]
# Build the model on training set
set.seed(123)
model <- train(
medv~., data = train.data, method = "pcr",
scale = TRUE,
trControl = trainControl("cv", number = 10),
tuneLength = 10
)
# Print the best tuning parameter ncomp that
# minimize the cross-validation error, RMSE
summary(model)
model$bestTune
我得到:
Data: X dimension: 407 13
Y dimension: 407 1
Fit method: svdpc
Number of components considered: 5
TRAINING: % variance explained
1 comps 2 comps 3 comps 4 comps 5 comps
X 47.48 58.40 68.00 74.75 80.94
.outcome 38.10 51.02 64.43 65.24 71.17
和
ncomp
5 5
谢谢。
-C.T
答案 0 :(得分:1)
根据我对PCR的了解,它对于减少维数很有用,特别是考虑到变量之间的共线性。一些有用的链接:
http://www.milanor.net/blog/performing-principal-components-regression-pcr-in-r/
http://www.win-vector.com/blog/2016/05/pcr_part1_xonly/
您执行的模型拟合过程并不是要告诉您要使用哪个原始变量进行预测,而是建议使用不同的系数集组合 all 个变量的各种方法,以得出元预测变量或“组件”的较短列表。
summary(model)
告诉您,使用单个成分可以解释38%的结果,但是使用5个成分可以解释71%的结果。 (bestTune
认为这是预测能力和模型简单性之间的最佳平衡。)
要查看这些组件的外观,可以查看model[["finalModel"]][["coefficients"]]
,其输出复制如下。我相信这显示了应用于每个变量的标准化版本以创建前几个组件中的每个组件的系数。
, , 1 comps # coefficients applied to variables to create component #1
.outcome
crim -0.592587562
zn 0.599992485
indus -0.802349361
chas 0.006248825
nox -0.791459829
rm 0.447008384
age -0.713823494
dis 0.739724065
rad -0.741112634
tax -0.785801427
ptratio -0.487355792
black 0.457723689
lstat -0.716345800
, , 2 comps # coefficients applied to variables to create component #2...
.outcome
crim -1.3071490
zn -0.1696660
indus -0.5509926
chas 1.3404699
nox -0.1278061
rm 1.2483213
age 0.2066896
dis -0.2710928
rad -1.3575348
tax -1.3611397
ptratio -1.5186875
black 0.8599876
lstat -1.0065236
, , 3 comps
.outcome
crim -0.379353935
zn 0.914275307
indus -0.663750247
chas 1.800225620
nox 0.173805923
rm 2.911531130
age 0.006938608
dis -0.221098241
rad -0.348203175
tax -0.583374313
ptratio -2.388449513
black -0.232188538
lstat -1.784086797
, , 4 comps
.outcome
crim -0.4650669
zn 0.8355077
indus -0.6614445
chas 1.0473459
nox 0.3090320
rm 3.0563403
age 0.1453604
dis -0.3965918
rad -0.4828702
tax -0.6739500
ptratio -2.6654286
black -0.3997876
lstat -1.8464750
, , 5 comps
.outcome
crim -0.56682510
zn 0.07089925
indus -0.72683562
chas 0.61226427
nox 0.03060117
rm 4.18612272
age 0.17409947
dis -0.70519697
rad 0.11136186
tax -0.31640619
ptratio -1.42839422
black 0.70692195
lstat -2.80938875
, , 6 comps
.outcome
crim -0.4526589
zn 0.2148831
indus -0.7151197
chas 0.5618245
nox 0.0850809
rm 4.1544127
age 0.2220764
dis -0.7142467
rad 0.1485075
tax -0.2614366
ptratio -1.5666266
black 1.0166062
lstat -2.7407352