有什么方法可以从 R 中的累积 PCA 图中 select 前 n 个 PCA 组件?
Any way to select top n PCA components from accumulative PCA plot in R?
我有兴趣从我的数据集的累积 PCA 图中选取前 10 个 PCA 组件。我设法获得了 PCA 图,例如 scree plot、pairs plot 等,但对我来说意义不大。所以我想从其累积的 PCA 图中 select 前 10 个 PCA 图,我做到了,但我需要使用这个前 10 个 PCA 组件对我的原始数据集进行子集化。谁能指出我如何使尝试更准确和更令人满意?
可重现数据:
persons_df <- data.frame(person1=sample(1:200,20, replace = FALSE),
person2=as.factor(sample(20)),
person3=sample(1:250,20, replace = FALSE),
person4=sample(1:300,20, replace = FALSE),
person5=as.factor(sample(20)),
person6=as.factor(sample(20)))
row.names(persons_df) <-letters[1:20]
我的尝试:
my_pca <- prcomp(t(persons_df), center=TRUE, scale=FALSE)
summary(my_pca)
my_pca_proportionvariances <- cumsum(((my_pca$sdev^2) / (sum(my_pca$sdev^2)))*100)
public 数据集:
因为我在创建上面的可重现数据时遇到了一些问题,所以我在这里链接了 public example dataset
这里我需要 select persons_df
的前 10 个 PCA 组件,然后对原始数据进行子集化,然后 运行 对其进行简单的线性回归。我怎样才能在这里完成我的方法以实现我的目标?任何人都可以在这里快速指出我吗?有什么想法吗?
使用PCA降维,简述:
- 省略你的输出变量(那是作弊)并在必要时用
model.matrix
创建对比变量。 (不要直接 one-hot 编码具有很多级别的因素,例如邮政编码,否则数据的大小会爆炸。聪明点。)删除任何 zero-variance 变量。处理 NA
s.
- 规模。一个大范围的变量(比如薪水)可以让其他一切看起来low-variance相比之下。
- 运行 PCA 与
princomp
或 prcomp
.
pca <- princomp(scale(cbind(mtcars[-1])))
- 要获得解释的方差百分比,请将
stdev
向量从 PCA 对象中拉出,将其平方以获得方差,然后按总和缩放,使其总和为 1。
pct_var_explained <- pca$sdev^2 / sum(pca$sdev^2)
pct_var_explained
#> Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6
#> 0.576021744 0.264964319 0.059721486 0.026950667 0.022225006 0.021011744
#> Comp.7 Comp.8 Comp.9 Comp.10
#> 0.013292009 0.008068158 0.005365235 0.002379633
- 查看已解释的累计方差和,了解您要保留多少主成分。例如,组件 9 和 10 在这里解释的方差不到 0.25%。您还可以使用
summary
为您进行这些计算。
cumsum(pct_var_explained)
#> Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7
#> 0.5760217 0.8409861 0.9007075 0.9276582 0.9498832 0.9708950 0.9841870
#> Comp.8 Comp.9 Comp.10
#> 0.9922551 0.9976204 1.0000000
summary(pca)
#> Importance of components:
#> Comp.1 Comp.2 Comp.3 Comp.4
#> Standard deviation 2.3622469 1.6021366 0.76062599 0.51096437
#> Proportion of Variance 0.5760217 0.2649643 0.05972149 0.02695067
#> Cumulative Proportion 0.5760217 0.8409861 0.90070755 0.92765822
#> Comp.5 Comp.6 Comp.7 Comp.8
#> Standard deviation 0.46400943 0.45116656 0.35884027 0.279571602
#> Proportion of Variance 0.02222501 0.02101174 0.01329201 0.008068158
#> Cumulative Proportion 0.94988322 0.97089497 0.98418697 0.992255132
#> Comp.9 Comp.10
#> Standard deviation 0.227981824 0.151831138
#> Proportion of Variance 0.005365235 0.002379633
#> Cumulative Proportion 0.997620367 1.000000000
- 对要保留的主要组件进行子集化,然后重新绑定输出变量。
train <- data.frame(
mpg = mtcars$mpg,
predict(pca)[, cumsum(pct_var_explained) < 0.95]
)
- 训练您的模型。
model <- lm(mpg ~ ., train)
summary(model)
#>
#> Call:
#> lm(formula = mpg ~ ., data = train)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -4.2581 -1.2933 -0.4999 1.3939 5.2861
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 20.09062 0.44345 45.305 < 2e-16 ***
#> Comp.1 -2.28131 0.18772 -12.153 3.17e-12 ***
#> Comp.2 0.11632 0.27679 0.420 0.6778
#> Comp.3 1.29925 0.58301 2.229 0.0347 *
#> Comp.4 -0.09002 0.86787 -0.104 0.9182
#> Comp.5 0.31279 0.95569 0.327 0.7461
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 2.509 on 26 degrees of freedom
#> Multiple R-squared: 0.8547, Adjusted R-squared: 0.8268
#> F-statistic: 30.59 on 5 and 26 DF, p-value: 4.186e-10
这个特定的模型几乎只需要 1 个主成分——其中有很多信息模型无能为力。 (也许它是不相关的、冗余的或非线性的。)迭代。
我有兴趣从我的数据集的累积 PCA 图中选取前 10 个 PCA 组件。我设法获得了 PCA 图,例如 scree plot、pairs plot 等,但对我来说意义不大。所以我想从其累积的 PCA 图中 select 前 10 个 PCA 图,我做到了,但我需要使用这个前 10 个 PCA 组件对我的原始数据集进行子集化。谁能指出我如何使尝试更准确和更令人满意?
可重现数据:
persons_df <- data.frame(person1=sample(1:200,20, replace = FALSE),
person2=as.factor(sample(20)),
person3=sample(1:250,20, replace = FALSE),
person4=sample(1:300,20, replace = FALSE),
person5=as.factor(sample(20)),
person6=as.factor(sample(20)))
row.names(persons_df) <-letters[1:20]
我的尝试:
my_pca <- prcomp(t(persons_df), center=TRUE, scale=FALSE)
summary(my_pca)
my_pca_proportionvariances <- cumsum(((my_pca$sdev^2) / (sum(my_pca$sdev^2)))*100)
public 数据集:
因为我在创建上面的可重现数据时遇到了一些问题,所以我在这里链接了 public example dataset
这里我需要 select persons_df
的前 10 个 PCA 组件,然后对原始数据进行子集化,然后 运行 对其进行简单的线性回归。我怎样才能在这里完成我的方法以实现我的目标?任何人都可以在这里快速指出我吗?有什么想法吗?
使用PCA降维,简述:
- 省略你的输出变量(那是作弊)并在必要时用
model.matrix
创建对比变量。 (不要直接 one-hot 编码具有很多级别的因素,例如邮政编码,否则数据的大小会爆炸。聪明点。)删除任何 zero-variance 变量。处理NA
s. - 规模。一个大范围的变量(比如薪水)可以让其他一切看起来low-variance相比之下。
- 运行 PCA 与
princomp
或prcomp
.
pca <- princomp(scale(cbind(mtcars[-1])))
- 要获得解释的方差百分比,请将
stdev
向量从 PCA 对象中拉出,将其平方以获得方差,然后按总和缩放,使其总和为 1。
pct_var_explained <- pca$sdev^2 / sum(pca$sdev^2)
pct_var_explained
#> Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6
#> 0.576021744 0.264964319 0.059721486 0.026950667 0.022225006 0.021011744
#> Comp.7 Comp.8 Comp.9 Comp.10
#> 0.013292009 0.008068158 0.005365235 0.002379633
- 查看已解释的累计方差和,了解您要保留多少主成分。例如,组件 9 和 10 在这里解释的方差不到 0.25%。您还可以使用
summary
为您进行这些计算。
cumsum(pct_var_explained)
#> Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7
#> 0.5760217 0.8409861 0.9007075 0.9276582 0.9498832 0.9708950 0.9841870
#> Comp.8 Comp.9 Comp.10
#> 0.9922551 0.9976204 1.0000000
summary(pca)
#> Importance of components:
#> Comp.1 Comp.2 Comp.3 Comp.4
#> Standard deviation 2.3622469 1.6021366 0.76062599 0.51096437
#> Proportion of Variance 0.5760217 0.2649643 0.05972149 0.02695067
#> Cumulative Proportion 0.5760217 0.8409861 0.90070755 0.92765822
#> Comp.5 Comp.6 Comp.7 Comp.8
#> Standard deviation 0.46400943 0.45116656 0.35884027 0.279571602
#> Proportion of Variance 0.02222501 0.02101174 0.01329201 0.008068158
#> Cumulative Proportion 0.94988322 0.97089497 0.98418697 0.992255132
#> Comp.9 Comp.10
#> Standard deviation 0.227981824 0.151831138
#> Proportion of Variance 0.005365235 0.002379633
#> Cumulative Proportion 0.997620367 1.000000000
- 对要保留的主要组件进行子集化,然后重新绑定输出变量。
train <- data.frame(
mpg = mtcars$mpg,
predict(pca)[, cumsum(pct_var_explained) < 0.95]
)
- 训练您的模型。
model <- lm(mpg ~ ., train)
summary(model)
#>
#> Call:
#> lm(formula = mpg ~ ., data = train)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -4.2581 -1.2933 -0.4999 1.3939 5.2861
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 20.09062 0.44345 45.305 < 2e-16 ***
#> Comp.1 -2.28131 0.18772 -12.153 3.17e-12 ***
#> Comp.2 0.11632 0.27679 0.420 0.6778
#> Comp.3 1.29925 0.58301 2.229 0.0347 *
#> Comp.4 -0.09002 0.86787 -0.104 0.9182
#> Comp.5 0.31279 0.95569 0.327 0.7461
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 2.509 on 26 degrees of freedom
#> Multiple R-squared: 0.8547, Adjusted R-squared: 0.8268
#> F-statistic: 30.59 on 5 and 26 DF, p-value: 4.186e-10
这个特定的模型几乎只需要 1 个主成分——其中有很多信息模型无能为力。 (也许它是不相关的、冗余的或非线性的。)迭代。