【原创】R语言多元线性回归特征降维:主成分回归和偏最小二乘案例分析报告附代码数据
【最新】R语言线性回归分析案例报告 附代码数据

R语言线性回归案例报告R初始指令安装“汽车”包:install.packages(“汽车”)加载库汽车加载汽车中的数据:数据(Salaries,package =“car”)查看您的办公桌上的数据(屏幕):它所表示的薪水视图(帮助)和数据描述:help(薪水)变量的确切名称:名称(薪金)考虑谁是定量和定性的变量分散图虽然有些变量不是量化的,但相反,它们是绝对的,例如秩序是有序的,我们将要制作离散图考虑到图表,我们将运行变量之间的简单回归模型:“yrs.since.phd”“yrs.service”,但首先让我们来回顾一下变量之间的相关性。
因此,我们要确定假设正态性的相关系数考虑到这两个变量之间的相关性高,解释结果结果因变量yrs.since.phd的选择是正确的,请解释为什么编写表单的模型:y = intercept + oendiente * x解释截距和斜率假设检验根据测试结果,考虑到p值= 2e-16,是否拒绝了5%的显着性值的假设?斜率为零根据测试结果,考虑到p值= 2e-16,关于斜率的假设是否被拒绝了5%的显着性值?考虑到变量yrs.service在模型中是重要的模型和测试调整考虑到R的平方值为0.827,你认为该模型具有良好的线性拟合?解释调整的R平方值考虑到验证数据与模型拟合的测试由F统计得到:1894在1和395 DF,p值:<2.2e-16认为模型符合调整? ##使用模型进行估计为变量x的以下值查找yrs.since.phd的估计值:Graficas del modeloValidación del modeloEn este caso se desea determinar los residuos(error)entre el modelo y lo observado y ver si los residuos cumplen con: 1. Tener media=0 2. Varianza= constante 3. Se distribuyen normal(0,constante) Los residuos se generan a continuación (por facilidad solo generamos los primeros 20 [1:20])Valores ajustados, es decir el valor de la variable y dad0 por el modelo (por facilidad solo generamos los primeros 20 [1:20])Prueba de normalidad, utilizamos la prueba QQ que permite deducir si los datos se ajustan a la normal (si los datos estan cerca de la línea)Validación del modeloEn este caso se desea determinar los residuos(error)entre el modelo y lo observado y ver si los residuos cumplen con: 1. Tener media=0 2. Varianza= constante 3. Se distribuyen normal(0,constante) Los residuos se generan a continuación (por facilidad solo generamos los primeros 20 [1:20])Valores ajustados, es decir el valor de la variable y dad0 por el modelo (por facilidad solo generamos los primeros 20 [1:20])Prueba de normalidad, utilizamos la prueba QQ que permite deducir si los datos se ajustan a la normal (si los datos estan cerca de la línea)condidera que los que estan a más de dos desviaciones estandar se distribuyen normal?。
r语言实现偏最小二乘法

r语言实现偏最小二乘法偏最小二乘法(Partial Least Squares Regression,简称PLSR)是一种多元统计分析方法,常用于建立预测模型。
在R语言中,我们可以使用plsr函数来实现偏最小二乘法。
在讲解具体实现之前,我们先来了解一下偏最小二乘法的原理。
偏最小二乘法是基于最小二乘法的一种改进方法,用于处理多重共线性问题。
在最小二乘法中,我们通过最小化预测值与真实值之间的平方误差来建立模型。
然而,在多重共线性存在的情况下,最小二乘法的结果可能会受到较大的误差影响。
偏最小二乘法通过将自变量和因变量进行正交变换,得到新的变量,从而消除了多重共线性的影响。
这些新的变量被称为偏最小二乘法的主成分,它们是原始变量的线性组合。
通过对主成分进行逐步回归分析,我们可以得到一个可靠的预测模型。
接下来,我们使用R语言中的plsr函数来实现偏最小二乘法。
首先,我们需要加载pls包,并读取我们的数据集。
假设我们的数据集包含了多个自变量和一个因变量。
```rlibrary(pls)data <- read.csv("data.csv")```然后,我们可以使用plsr函数来建立偏最小二乘法模型。
在plsr 函数中,我们需要指定自变量和因变量的列数,并选择主成分的数量。
```rmodel <- plsr(Y ~ X1 + X2 + X3, data = data, ncomp = 2)```在上述代码中,Y代表因变量,X1、X2、X3代表自变量。
我们选择了2个主成分。
接下来,我们可以使用summary函数来查看模型的摘要信息。
```rsummary(model)```摘要信息中会显示出模型的R方值、标准误差以及每个主成分的贡献率。
我们还可以使用plot函数来绘制模型的各个主成分的贡献率图。
```rplot(model, ncomp = 2, asp = 1)```在图中,横轴代表主成分的数量,纵轴代表贡献率。
【原创】R语言 多元统计分析介绍数据分析数据挖掘案例报告(附代码

#第十章多元统计分析介绍#10.1 主成分分析与因子分析#10.1.1 主成分的简要定义与计算#10.1.2 主成分R 通用程序student<-data.frame(X1=c(148, 139, 160, 149, 159, 142, 153, 150, 151, 139, 140, 161, 158, 140, 137, 152, 149, 145, 160, 156,151, 147, 157, 147, 157, 151, 144, 141, 139, 148),X2=c(41, 34, 49, 36, 45, 31, 43, 43, 42, 31,29, 47, 49, 33, 31, 35, 47, 35, 47, 44,42, 38, 39, 30, 48, 36, 36, 30, 32, 38),X3=c(72, 71, 77, 67, 80, 66, 76, 77, 77, 68,64, 78, 78, 67, 66, 73, 82, 70, 74, 78,73, 73, 68, 65, 80, 74, 68, 67, 68, 70),X4=c(78, 76, 86, 79, 86, 76, 83, 79, 80, 74,74, 84, 83, 77, 73, 79, 79, 77, 87, 85,82, 78, 80, 75, 88, 80, 76, 76, 73, 78))student.pr<-princomp(student, cor=TRUE)summary(student.pr,loadings=TRUE)#10.1.3 因子分析的简要定义与计算#10.1.4 因子分析R 通用程序student<-read.table("e:/data/student.txt")names(student)=c("math", "phi", "chem", "lit", "his", "eng") fa<-factanal(student, factors=2)fa#10.2 判别分析#10.2.1 距离判别#10.2.2 Fisher 判别法#10.2.3 R 通用程序library(MASS)data(iris)attach(iris)names(iris)library(MASS)iris.lda <- lda(Species ~ Sepal.Length + Sepal.Width+ Petal.Length + Petal.Width)【原创】定制代写开发r/python/spss/matlab/WEKA/sas/sql/C++/stata/eviews数据挖掘和统计分析可视化调研报告程序等服务(附代码数据),咨询:3025393450@有问题到淘宝找“大数据部落”就可以了iris.ldairis.pred=predict(iris.lda) $ classtable(iris.pred, Species)detach(iris)w <- read.table("e:/data/disc.txt")names(w)=c("group", "x1", "x2", "x3", "x4")library(MASS)z <- lda(group~x1+x2+x3+x4, data=w, prior=c(1, 1)/2) newdata<-rbind(c(8.85, 3.38, 5.17, 26.10), c(28.60, 2.40, 1.20, 127.0),c(20.70, 6.70, 7.60, 30.20), c(7.90, 2.40, 4.30, 33.20),c(3.19, 3.20, 1.43, 9.90), c(12.40, 5.10, 4.43, 24.60),c(16.80, 3.40, 2.31, 31.30), c(15.00, 2.70, 5.02, 64.00)) dimnames(newdata)<-list(NULL, c("x1", "x2", "x3", "x4")) newdata<-data.frame(newdata)predict(z, newdata=newdata)#10.3 聚类分析#10.3.1 基本思想#10.3.2 R通用程序x<-c(1, 2, 4.5, 6, 8)dim(x)<-c(5, 1)d<-dist(x)hc1<-hclust(d, "single")hc2<-hclust(d, "complete")hc3<-hclust(d, "median")hc4<-hclust(d, "ward")opar<-par(mfrow=c(2, 2))plot(hc1, hang=-1);plot(hc2, hang=-1)plot(hc3, hang=-1);plot(hc4, hang=-1)par(opar)data(iris);attach(iris)iris.hc<-hclust(dist(iris[,1:4]))plot(iris.hc, hang = -1)plclust(iris.hc,labels = FALSE, hang=-1)re<-rect.hclust(iris.hc,k=3)iris.id <- cutree(iris.hc,3)table(iris.id,Species)#10.4 典型相关分析#10.4.1 基本思想#10.4.2 R通用程序invest=read.table("e:/data/invest.txt")names(invest)=c("x1", "x2", "x3", "x4", "x5", "x6", "y1", "y2", "y3", "y4", "y5")ca<-cancor(invest[, 1:6], invest[, 7:11])ca#x10.5 对应分析#10.5.1 基本思想#10.5.2 R通用程序x.df=data.frame(HighlyFor=c(2, 6, 41, 72, 24), For =c(17, 65, 220, 224, 61),Against=c(17, 79, 327, 503, 300), HighlyAgainst=c(5, 6, 48, 47, 41))rownames(x.df)<-c("BelowPrimary", "Primary", "Secondary", "HighSchool","College")library(MASS)biplot(corresp(x.df, nf=2))。
R语言线性回归数据分析案例可视化报告 (附代码数据)

R语言线性回归数据分析案例可视化报告从源下载数据集。
2.清理数据2.a放入数据列pimalm<-lm(class~npreg+glucose+bp+triceps+insulin+bmi+dia betes+age, data=pima)去除大p值的变量(p值> 0.005)Remove variables (insulin, age) with large p value (p value > 0.005) After the variables are dropped, the R-squared value remain about the same. This suggests the variables dropped do not have much effect on the model.Residual analysis shows almost straight line with distribution around zero. Due to this pattern, this model is not as robust.qqnorm(resid(pimalm), col="blue")qqline(resid(pimalm), col="red")The second dataset with much simpler variables. Although intuitively the variables both effect the output, the amount of effect by each variable is interesting. This dataset was examined to have a better sense of how multivariate regression will perform.allbacks.lm<-lm(weight~volume+area, data=allbacks) summary(allbacks.lm)qqnorm(resid(allbacks.lm), col="blue") qqline(resid(allbacks.lm), col="red")。
基于主成分分析的R语言在多元数据降维中的应用研究

基于主成分分析的R语言在多元数据降维中的应用研究近年来,随着大数据时代的到来,我们面临的数据量越来越庞大。
在这样的背景下,如何从海量的数据中提取出有意义,且具有参考价值的信息,成为了数据科学和机器学习领域的主要挑战之一。
为了应对这一挑战,许多数据降维的方法被提出并得到了广泛应用。
在数据降维的方法中,主成分分析(Principal Component Analysis,简称PCA)是其中最具代表性的一种方法。
PCA通过线性变换将高维的特征空间映射到低维的特征空间中,从而实现数据降维。
其基本思想在于找到能够最大程度保留原始数据信息的特征轴,将其作为新的特征空间的主成分,从而达到数据降维的目的。
在R语言中,我们可以使用现有的函数库来实现主成分分析。
R语言是一门功能强大的数据分析和统计建模的编程语言,在数据科学和机器学习领域非常受欢迎。
下面,我们将详细介绍在多元数据降维中如何使用R语言进行基于主成分分析的应用研究。
首先,我们需要加载R语言中的相关函数库,以便使用主成分分析算法。
可以使用以下代码加载常用的函数库:```R# 加载常用函数库library(stats) # 统计函数库library(factoextra) # PCA可视化函数库```接下来,我们需要准备数据集,作为主成分分析的输入。
在多元数据降维中,我们通常使用一个矩阵或数据框来表示多个变量的数据。
假设我们有一个数据集`data`,其中包含了`n`个样本和`p`个变量。
可以使用以下代码读取数据集:```R# 读取数据集data <- read.csv("data.csv") # 将文件名替换为实际的数据文件名```在数据准备完成后,我们可以使用`prcomp()`函数进行主成分分析。
该函数会返回一个PCA对象,其中包含了主成分分析的结果。
可以通过以下代码应用主成分分析:```R# 进行主成分分析pca <- prcomp(data, scale = TRUE) # 对数据进行标准化处理# 查看主成分分析结果print(pca)```在主成分分析完成后,我们可以使用`fviz_eig()`函数来可视化PCA的方差解释比例。
R语言数据分析回归研究案例报告 附代码数据

R语言数据分析回归研究案例:移民政策偏好是否有准确的刻板印象?数据重命名,重新编码,重组Group <chr> Count<dbl>Percent<dbl>6 476 56.00 5 179 21.062 60 7.063 54 6.354 46 5.41 1 27 3.18 0 8 0.94对Kirkegaard&Bjerrekær2016的再分析确定用于本研究的32个国家的子集的总体准确性。
#降低样本的#精确度GG_scatter(dk_fiscal, "mean_estimate", "dk_benefits_use",GG_scatter(dk_fiscal_sub, "mean_estimate", "dk_benefits_us e", case_names="Names")GG_scatter(dk_fiscal, "mean_estimate", "dk_fiscal", case_n ames="Names")#compare Muslim bias measures#can we make a bias measure that works without ratio scaleScore stereotype accuracy#add metric to main datad$stereotype_accuracy=indi_accuracy$pearson_rGG_save("figures/aggr_retest_stereotypes.png")GG_save("figures/aggregate_accuracy.png")GG_save("figures/aggregate_accuracy_no_SYR.png")Muslim bias in aggregate dataGG_save("figures/aggregate_muslim_bias.png")Immigrant preferences and stereotypesGG_save("figures/aggregate_muslim_bias_old_data.png") Immigrant preferences and stereotypesGG_save("figures/aggr_fiscal_net_opposition_no_SYR.png")GG_save("figures/aggr_stereotype_net_opposition.png")GG_save("figures/aggr_stereotype_net_opposition_no_SYR.pn g")lhs <chr>op<chr > rhs <chr> est <dbl> se <dbl> z <dbl> pvalue <dbl> net_opposition ~ mean_estimate_fiscal -4.4e-01 0.02303 -19.17 0.0e+00net_opposition~Muslim_frac 4.3e-02 0.05473 0.79 4.3e-01net_opposition~~net_opposition 6.9e-03 0.00175 3.94 8.3e-05dk_fiscal ~~ dk_fiscal 6.2e+03 0.00000 NA NAMuslim_frac~~Muslim_frac1.7e-01 0.0000NANAIndividual level modelsGG_scatter(example_muslim_bias, "Muslim", "resid", case_na mes="name")+#exclude Syria#distributiondescribe(d$Muslim_bias_r)%>%print()GG_save("figures/muslim_bias_dist.png")## `stat_bin()` using `bins = 30`. Pick better value with `GG_scatter(mediation_example, "Muslim", "resid", case_name s="name", repel_names=T)+scale_x_continuous("Muslim % in home country", labels=scal#stereotypes and preferencesmediation_model=plyr::ldply(seq_along_rows(d), function(rGG_denhist(mediation_model, "Muslim_resid_OLS", vline=medi an)## `stat_bin()` using `bins = 30`. Pick better value with `add to main datad$Muslim_preference=mediation_model$Muslim_resid_OLS Predictors of individual primary outcomes#party modelsrms::ols(stereotype_accuracy~party_vote, data=d)GG_group_means(d, "Muslim_bias_r", "party_vote")+ theme(axis.text.x=element_text(angle=-30, hjust=0))GG_group_means(d, "Muslim_preference", "party_vote")+#party agreement cors wtd.cors(d_parties)。
【原创】R语言线性回归案例数据分析可视化报告(附代码数据)

R语言线性回归案例数据分析可视化报告在本实验中,我们将查看来自所有30个职业棒球大联盟球队的数据,并检查一个赛季的得分与其他球员统计数据之间的线性关系。
我们的目标是通过图表和数字总结这些关系,以便找出哪个变量(如果有的话)可以帮助我们最好地预测一个赛季中球队的得分情况。
数据用变量at_bats绘制这种关系作为预测。
关系看起来是线性的吗?如果你知道一个团队的at_bats,你会习惯使用线性模型来预测运行次数吗?散点图.如果关系看起来是线性的,我们可以用相关系数来量化关系的强度。
.残差平方和回想一下我们描述单个变量分布的方式。
回想一下,我们讨论了中心,传播和形状等特征。
能够描述两个数值变量(例如上面的runand at_bats)的关系也是有用的。
从前面的练习中查看你的情节,描述这两个变量之间的关系。
确保讨论关系的形式,方向和强度以及任何不寻常的观察。
正如我们用均值和标准差来总结单个变量一样,我们可以通过找出最符合其关联的线来总结这两个变量之间的关系。
使用下面的交互功能来选择您认为通过点云的最佳工作的线路。
# Click two points to make a line.After running this command, you’ll be prompted to click two points on the plot to define a line. Once you’ve done that, the line you specified will be shown in black and the residuals in blue. Note that there are 30 residuals, one for each of the 30 observations. Recall that the residuals are the difference between the observed values and the values predicted by the line:e i=y i−y^i ei=yi−y^iThe most common way to do linear regression is to select the line that minimizes the sum of squared residuals. To visualize the squared residuals, you can rerun the plot command and add the argument showSquares = TRUE.## Click two points to make a line.Note that the output from the plot_ss function provides you with the slope and intercept of your line as well as the sum of squares.Run the function several times. What was the smallest sum of squares that you got? How does it compare to your neighbors?Answer: The smallest sum of squares is 123721.9. It explains the dispersion from mean. The linear modelIt is rather cumbersome to try to get the correct least squares line, i.e. the line that minimizes the sum of squared residuals, through trial and error. Instead we can use the lm function in R to fit the linear model (a.k.a. regression line).The first argument in the function lm is a formula that takes the form y ~ x. Here it can be read that we want to make a linear model of runs as a function of at_bats. The second argument specifies that R should look in the mlb11 data frame to find the runs and at_bats variables.The output of lm is an object that contains all of the information we need about the linear model that was just fit. We can access this information using the summary function.Let’s consider this output piece by piece. First, the formula used to describe the model is shown at the top. After the formula you find the five-number summary of the residuals. The “Coefficients” table shown next is key; its first column displays the linear model’s y-intercept and the coefficient of at_bats. With this table, we can write down the least squares regression line for the linear model:y^=−2789.2429+0.6305∗atbats y^=−2789.2429+0.6305∗atbatsOne last piece of information we will discuss from the summary output is the MultipleR-squared, or more simply, R2R2. The R2R2value represents the proportion of variability in the response variable that is explained by the explanatory variable. For this model, 37.3% of the variability in runs is explained by at-bats.output, write the equation of the regression line. What does the slope tell us in thecontext of the relationship between success of a team and its home runs?Answer: homeruns has positive relationship with runs, which means 1 homeruns increase 1.835 times runs.Prediction and prediction errors Let’s create a scatterplot with the least squares line laid on top.The function abline plots a line based on its slope and intercept. Here, we used a shortcut by providing the model m1, which contains both parameter estimates. This line can be used to predict y y at any value of x x. When predictions are made for values of x x that are beyond the range of the observed data, it is referred to as extrapolation and is not usually recommended. However, predictions made within the range of the data are more reliable. They’re also used to compute the residuals.many runs would he or she predict for a team with 5,578 at-bats? Is this an overestimate or an underestimate, and by how much? In other words, what is the residual for thisprediction?Model diagnosticsTo assess whether the linear model is reliable, we need to check for (1) linearity, (2) nearly normal residuals, and (3) constant variability.Linearity: You already checked if the relationship between runs and at-bats is linear using a scatterplot. We should also verify this condition with a plot of the residuals vs. at-bats. Recall that any code following a # is intended to be a comment that helps understand the code but is ignored by R.6.Is there any apparent pattern in the residuals plot? What does this indicate about the linearity of the relationship between runs and at-bats?Answer: the residuals has normal linearity of the relationship between runs ans at-bats, which mean is 0.Nearly normal residuals: To check this condition, we can look at a histogramor a normal probability plot of the residuals.7.Based on the histogram and the normal probability plot, does the nearly normal residuals condition appear to be met?Answer: Yes.It’s nearly normal.Constant variability:1. Choose another traditional variable from mlb11 that you think might be a goodpredictor of runs. Produce a scatterplot of the two variables and fit a linear model. Ata glance, does there seem to be a linear relationship?Answer: Yes, the scatterplot shows they have a linear relationship..1.How does this relationship compare to the relationship between runs and at_bats?Use the R22 values from the two model summaries to compare. Does your variable seem to predict runs better than at_bats? How can you tell?1. Now that you can summarize the linear relationship between two variables, investigatethe relationships between runs and each of the other five traditional variables. Which variable best predicts runs? Support your conclusion using the graphical andnumerical methods we’ve discussed (for the sake of conciseness, only include output for the best variable, not all five).Answer: The new_obs is the best predicts runs since it has smallest Std. Error, which the points are on or very close to the line.1.Now examine the three newer variables. These are the statistics used by the author of Moneyball to predict a teams success. In general, are they more or less effective at predicting runs that the old variables? Explain using appropriate graphical andnumerical evidence. Of all ten variables we’ve analyzed, which seems to be the best predictor of runs? Using the limited (or not so limited) information you know about these baseball statistics, does your result make sense?Answer: ‘new_slug’ as 87.85% ,‘new_onbase’ as 77.85% ,and ‘new_obs’ as 68.84% are predicte better on ‘runs’ than old variables.1. Check the model diagnostics for the regression model with the variable you decidedwas the best predictor for runs.This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was adapted for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel from a lab written by the faculty and TAs of UCLA Statistics.。
R语言实现偏最小二乘回归法partialleastsquares(PLS)回归

R语言实现偏最小二乘回归法partialleastsquares(PLS)回归原文链接:/?p=8652偏最小二乘回归是一种回归形式。
当使用pls时,新的线性组合有助于解释模型中的自变量和因变量。
在本文中,我们将使用pls在“ Mroz”数据集中预测“收入”。
library(pls);library(Ecdat)data("Mroz")str(Mroz)## 'data.frame': 753 obs. of 18 variables:## $ work : Factor w/ 2 levels "yes","no": 2 2 2 22 2 2 2 22 ...## $ hoursw : int 1610 16561980 4561568 20321440 1 0201458 1600 ...## $ child6 : int 1 0 1 01 0 0 000 ...## $ child618 : int 0 2 3 32 0 2 022 ...## $ agew : int 32 30 35 3431 54 37 544839 ...## $ educw : int 12 12 12 1214 12 16 121212 ...## $ hearnw : num 3.35 1.394.55 1.14.59 ...## $ wagew : num 2.65 2.654.04 3.253.6 4.75.95 9.980 4.15 ...## $ hoursh : int 2708 23103072 19202000 10402670 4 1201995 2100 ...## $ ageh : int 34 30 40 5332 57 37 535243 ...## $ educh : int 12 9 12 1012 11 12 8412 ...## $ wageh : num 4.03 8.443.58 3.5410 ...## $ income : int 16310 2180021040 730027300 19495 21152 1890020405 20425 ...## $ educwm : int 12 7 12 712 14 14 377 ...## $ educwf : int 7 7 7 714 7 7 377 ...## $ unemprate : num 5 11 5 59.5 7.55 5 3 5 ...## $ city : Factor w/ 2 levels "no","yes": 1 2 1 12 2 1 11 1 ...## $ experience: int 14 5 15 67 33 11 352421 ...首先,我们必须通过将数据分为训练和测试集来准备数据。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
主成分分析(Principal Component Analysis, PCA)
一个优秀的模型应该是用尽可能少的特征来涵盖尽可能多的信息。
对于多元线性回归模型,除了对全部特征进行筛选和压缩——这些都是针对原特征本身,那么是否可以把多个特征组合成少数的几个新特征,使模型更加简洁?特别是多个特征之间往往还存在多重共线性关系。
主成分分析的核心思想就是降维,把高维空间上的多个特征组合成少数几个无关的主成分,同时包含原数据中大部分的变异信息。
举个例子,在二维平面中,如果大部分的点都在一条直线附近,是不是就可以直接用这条直线当作一维坐标轴来反映原始数据?在三维空间中,如果大部分的点都在一个平面附近,是不是就可以直接用这个平面当作二维平面来反映原始数据?
以上概念中的变异信息就用方差来衡量,第一主成分是高维空间上的一个向量,所有的点沿着这条线波动最大,或者说所有的点到直线的距离的平方和最小。
如下图所示,所有的点沿着绿色直线的波动最大,它就代表着第一主成分向量。
主成分分析几何意义
有了第一主成分,还可以依次往后选择主成分,各主成分之间是相互正交的向量。
如下左图所示,右图是左图的旋转,以第一主成分作为x轴,第二主成分作为y轴与之垂直。
第一主成分与第二主成分
以上是主成分的几何意义,那么它的数学意义是什么?如何去求解主成分向量?
首先,我们定义主成分是原特征的线性组合,即:
主成分定义
找到一组Φ(其平方和为1),使Z1的方差最大,它的优化问题变成:
主成分优化目标
第一主成分确定之后,如果是二维空间那么第二主成分就可以通过正交关系直接确定;对于高维空间,一个向量的正交向量可以有无数个,则在其正交向量中继续优化上式至最大值;之后的主成分依次类推。
以下以美国的犯罪记录数据为例,对特征进行PCA:
> library(ISLR)
> apply(USArrests,2,mean) # 各变量均值差异很大
Murder Assault UrbanPop Rape
7.788170.76065.54021.232
> apply(USArrests,2,var) # 各变量方差差异也很大
Murder Assault UrbanPop Rape
18.970476945.16571209.5187887.72916
>
> ## 必须要对变量进行标准化,否则主成分就由Assault一个变量决定
>
> pr.out = prcomp(USArrests,scale. = T)
> names(pr.out)
[1] "sdev""rotation""center""scale""x"
> pr.out$center # 标准化之后的均值
主成分分析双坐标图
这张图是主成分分析的双坐标图,略复杂。
这里解释一下:这个图是二维的,所以它只展示第一和第二主成分,PC1和PC2侧的坐标轴(即矩形下方和左方两个)映射的是主成分得分,而上侧和右侧的两个坐标轴(即刻度是红色的)映射的主成分负荷。
图中每一个黑色的单词代表一个观测数据,它的位置对应着该观测数据在第一和第二主成分上的得分或负荷。
红色的箭头是各个特征在第一和第二主成分上的负荷向量,该向量在右上侧的坐标系下的投影即为主成分负荷。
从这张图中能看出什么?
•Assault、Murder、Rape这三个特征在第一主成分负荷向量上的权重大致相同,而UrbanPop的权重较小;•UrbanPop在第二主成分负荷向量上的权重较大,而其他三个特征权重较小;
•三个与犯罪相关的变量位置较近,相关性强;UrbanPop与其他三个变量相关性较弱。
接下来就看各个主成分所占方差比(即解释数据变异的程度),来确定选择几个主成分。
> pr.var = pr.out$sdev ^ 2# 样本在四个主成分的方差
> pr.var
[1] 2.48024160.98976520.35656320.1734301
> pve = pr.var/sum(pr.var) # 各主成分的方差解释比例
> pve # 第一主成分解释了数据62%的方差,第二主成分解释了24.7%的方差
[1] 0.620060390.247441290.089140800.04335752
>
> plot(pve,xlab="Principal Component",ylab = "Proportion of Variance Explained",ylim = c(0,1),type="b")
> plot(cumsum(pve),xlab="Principal Component",ylab = "Cumulative Proportion of Variance Explained",ylim = c(0,1),type="b")
主成分解释方差比
主成分累积解释方差比
由图中可见,选择前两个或前三个主成分,已经足以解释数据大部分的变异信息了。
主成分回归(Principal Component Regression, PCR)
所谓主成分回归,就是基于以筛选出的主成分作为特征来构建回归模型。
这里依然以Hitters数据集为例,构建PCR模型。
library(ISLR)library(pls)
Hitters = na.omit(Hitters)
x = model.matrix(Salary~.,Hitters)[,-1]。