专栏名称: 爱数据原统计网
中国统计网(www.itongji.cn),国内最大的数据分析门户网站。提供数据分析行业资讯,统计百科知识、数据分析、商业智能(BI)、数据挖掘技术,Excel、SPSS、SAS、R等数据分析软件等在线学习平台。
目录
相关文章推荐
51好读  ›  专栏  ›  爱数据原统计网

R语言多元统计分析初探

爱数据原统计网  · 公众号  · BI  · 2016-12-27 17:20

正文



读取多元统计分析数据到R


wine

绘制多元统计数据

矩阵散点图


一种常见的方法是使用散点图画出多元统计数据,展现出所有变量两两之间的散点图。


我们可以使用R中的“car”包里的“scatterplotMatrix()”函数来实现。

library(car)
scatterplotMatrix(wine[2:6])

组群标注数据点的散点图
plot(wine$V4,wine$V5)
text(wine$V4,wine$V5,wine$V1,cex=0.7,pos=4,col="red")

轮廓图?


轮廓图? 另一种非常有用的图表类型便是”轮廓图”,它通过绘制出每个变量在样本中的值,展示出每个变量的变化。

下文的“makeProfilePlot()”函数可以绘制出轮廓图。这个函数需要“RColorBrewer”库。

makeProfilePlot
require(RColorBrewer)
# find out how many variables we want to include
numvariables
# choose 'numvariables' random colours
colours
# find out the minimum and maximum values of the variables:
mymin
mymax
for(i in 1:numvariables){
vectori
mini
maxi
if(mini
if(maxi>mymax) {mymax
}

# plot the variables
for(i in 1:numvariables){
vectori
namei
colouri

if(i == 1) {plot(vectori,col=colouri,type="l",ylim=c(mymin,mymax))}
else  {points(vectori,col=colouri,type="l")}

lastxval
lastyval
text((lastxval-10),(lastyval),namei,col="black",cex=0.6)
}
}

例如,为了画出葡萄酒样本中前五种化学物质的轮廓图(他们存储在“wine”变量的V2,V2,V4,V5,V6列),我们输入:

library(RColorBrewer)
names
mylist
makeProfilePlot(mylist,names)

计算多元统计数据的概要统计量

另一件事便是你可能会想计算你的多元统计数据集中每一个变量的概要统计量,像均值、标准偏差之类。

sapply(wine[,2:14],mean)
sapply(wine[,2:14],sd)

我们可以通过标准化来使数据看起来更有意义,以使我们能清楚的比较这些变量。我们需要便准化每一个变量以便使他们样本方差为1,样本均值为0.

每组的均值与方差

通常感兴趣于从一个特定样本群体去计算其均值和标准偏差,例如,计算每一个品种葡萄酒样本。葡萄酒品种被存储在“wine”变量的“V1”列中。

为了仅提取2号品种的数据,我们输入:

cultivar2wine
sapply(cultivar2wine[2:14],mean)
sapply(cultivar2wine[2:14],sd)

你也可以通过相似的方法计算1号品种样本,或者是3号品种样本的13种化学物质浓度的均值和标准偏差:

然而,为了方便起见,你也许想通过以下的“printMeanAndSdByGroup()”函数一次性输出数据集中分组数据的均值和标准偏差:

printMeanAndSdByGroup
# find the names of the variables
variablenames
# within each group, find the mean of each variable
groupvariable
means
names(means)
print(paste("Mean:"))
print(means)
# within each group, find the standard deviation of each variable:
sds
names(sds)
print(paste("Standard deviations:"))
print(sds)
# within each group, find the number of samples:
samplesizes
names(samplesizes)
print(paste("Sample sizes:"))
print(samplesizes)
}
printMeanAndSdByGroup(wine[2:14],wine[1])

函数”printMeanAndSdByGroup()”将输出分组样本的数字。在本例中,我们可以看到品种1有59个样本,品种2有71个样本,品种3有48个样本。

变量的组间方差和组内方差

如果我们想计算特定变量的组内方差(例如,计算特定化学物质的浓度),我们可以使用下述的“calWithinGroupsVariance()”函数:

calcWithinGroupsVariance
# find out how many values the group variable can take
groupvariable2
levels
numlevels
# get the mean and standard deviation for each group:
numtotal
denomtotal
for(i in 1:numlevels){
leveli
levelidata
levelilength
# get the mean and standard deviation for group i:
meani
sdi
numi
denomi
numtotal
denomtotal
}
# calculate the within-groups variance
Vw
return(Vw)
}

例如,计算V2变量(第一种化学物质的浓度)的组内方差,我们输入:
calcWithinGroupsVariance(wine[2],wine[1]) # [1] 0.2620525

我们可以通过下述的“calcBetweenGroupsVariance()”函数来计算特定变量(如V2)的组间方差:

calcBetweenGroupsVariance
# find out how many values the group variable can take
groupvariable2
levels
numlevels
# calculate the overall grand mean:
grandmean
# get the mean and standard deviation for each group:
numtotal
denomtotal
for (i in 1:numlevels)
{
leveli
levelidata
levelilength
# get the mean and standard deviation for group i:
meani
sdi
numi
denomi
numtotal
denomtotal
}







请到「今天看啥」查看全文