R语言多元统计分析初探
admin
2023-02-16 04:20:05
0

# 读取多元统计分析数据到R
wine<-read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep=",")
# 绘制多元统计数据
# 矩阵散点图
# 一种常见的方法是使用散点图画出多元统计数据,展现出所有变量两两之间的散点图。
# 我们可以使用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<-function(mylist,names){
  require(RColorBrewer)
  # find out how many variables we want to include
  numvariables<-length(mylist)
  # choose 'numvariables' random colours
  colours<-brewer.pal(numvariables,"Set1")
  # find out the minimum and maximum values of the variables:
  mymin<-1e+20
  mymax<-1e-20
  for(i in 1:numvariables){
    vectori<-mylist`i`
    mini<-min(vectori)
    maxi<-max(vectori)
    if(mini
    if(maxi>mymax) {mymax<-maxi}
  }
  
  # plot the variables
  for(i in 1:numvariables){
    vectori<-mylist`i`
    namei<-names[i]
    colouri<-colours[i]
    
    if(i == 1) {plot(vectori,col=colouri,type="l",ylim=c(mymin,mymax))}
    else  {points(vectori,col=colouri,type="l")}
    
    lastxval<-length(vectori)
    lastyval<-vectori[length(vectori)]
    text((lastxval-10),(lastyval),namei,col="black",cex=0.6)
  }
}
# 例如,为了画出葡萄酒样本中前五种化学物质的轮廓图(他们存储在“wine”变量的V2,V2,V4,V5,V6列),我们输入:
library(RColorBrewer)
names<-c("V2","V3","V4","V5","V6")
mylist<-list(wine$V2,wine$V3,wine$V4,wine$V5,wine$V6)
makeProfilePlot(mylist,names)


# 计算多元统计数据的概要统计量
# 另一件事便是你可能会想计算你的多元统计数据集中每一个变量的概要统计量,像均值、标准偏差之类。
sapply(wine[,2:14],mean)
sapply(wine[,2:14],sd)
# 我们可以通过标准化来使数据看起来更有意义,以使我们能清楚的比较这些变量。我们需要便准化每一个变量以便使他们样本方差为1,样本均值为0.


# 每组的均值与方差
# 通常感兴趣于从一个特定样本群体去计算其均值和标准偏差,例如,计算每一个品种葡萄酒样本。葡萄酒品种被存储在“wine”变量的“V1”列中。
# 为了仅提取2号品种的数据,我们输入:
cultivar2wine<-wine[wine$V1==2,]
sapply(cultivar2wine[2:14],mean)
sapply(cultivar2wine[2:14],sd)
# 你也可以通过相似的方法计算1号品种样本,或者是3号品种样本的13种化学物质浓度的均值和标准偏差: 
# 然而,为了方便起见,你也许想通过以下的“printMeanAndSdByGroup()”函数一次性输出数据集中分组数据的均值和标准偏差:
printMeanAndSdByGroup<-function(variables,groupvariable){
  # find the names of the variables
  variablenames<-c(names(groupvariable),names(as.data.frame(variables)))
  # within each group, find the mean of each variable
  groupvariable<-groupvariable[,1] #ensures groupvariable is not a list
  means<-aggregate(as.matrix(variables)~groupvariable,FUN=mean)
  names(means)<-variablenames
  print(paste("Mean:"))
  print(means)
  # within each group, find the standard deviation of each variable:
  sds<-aggregate(as.matrix(variables)~groupvariable,FUN=sd)
  names(sds)<-variablenames
  print(paste("Standard deviations:"))
  print(sds)
  # within each group, find the number of samples:
  samplesizes<-aggregate(as.matrix(variables)~groupvariable,FUN=length)
  names(samplesizes)<-variablenames
  print(paste("Sample sizes:"))
  print(samplesizes)
}
printMeanAndSdByGroup(wine[2:14],wine[1])
# 函数”printMeanAndSdByGroup()”将输出分组样本的数字。在本例中,我们可以看到品种1有59个样本,品种2有71个样本,品种3有48个样本。


## 变量的组间方差和组内方差
# 如果我们想计算特定变量的组内方差(例如,计算特定化学物质的浓度),我们可以使用下述的“calWithinGroupsVariance()”函数:
calcWithinGroupsVariance<-function(variable,groupvariable){
  # find out how many values the group variable can take
  groupvariable2<-as.factor(groupvariable`1`)
  levels<-levels(groupvariable2)
  numlevels<-length(levels)
  # get the mean and standard deviation for each group:
  numtotal<-0
  denomtotal<-0
  for(i in 1:numlevels){
    leveli<-levels[i]
    levelidata<-variable[groupvariable==leveli,]
    levelilength<-length(levelidata)
    # get the mean and standard deviation for group i:
    meani<-mean(levelidata)
    sdi<-sd(levelidata)
    numi<-(levelilength-1)*(sdi*sdi)
    denomi<-levelilength
    numtotal<-numtotal+numi
    denomtotal<-denomtotal+denomi
  }
  # calculate the within-groups variance
  Vw<-numtotal/(denomtotal-numlevels)
  return(Vw)
}
# 例如,计算V2变量(第一种化学物质的浓度)的组内方差,我们输入:
calcWithinGroupsVariance(wine[2],wine[1]) # [1] 0.2620525
# 我们可以通过下述的“calcBetweenGroupsVariance()”函数来计算特定变量(如V2)的组间方差:
calcBetweenGroupsVariance <- function(variable,groupvariable) {
  # find out how many values the group variable can take 
  groupvariable2 <- as.factor(groupvariable`1`) 
  levels <- levels(groupvariable2)
  numlevels <- length(levels) 
  # calculate the overall grand mean: 
  grandmean <- mean(variable[,1]) 
  # get the mean and standard deviation for each group: 
  numtotal <- 0
  denomtotal <- 0 
  for (i in 1:numlevels) 
  {
    leveli <- levels[i] 
    levelidata <- variable[groupvariable==leveli,] 
    levelilength <- length(levelidata) 
    # get the mean and standard deviation for group i:
    meani <- mean(levelidata) 
    sdi <- sd(levelidata) 
    numi <- levelilength * ((meani - grandmean)^2)
    denomi <- levelilength 
    numtotal <- numtotal + numi 
    denomtotal <- denomtotal + denomi
  } 
  # calculate the between-groups variance 
  Vb <- numtotal / (numlevels - 1) 
  Vb <- Vb`1`
  return(Vb) 
}
# 可以像这样使用它计算V2的组间方差:
calcBetweenGroupsVariance(wine[2],wine[1]) # [1] 35.39742
# 我们可以通过变量的组间方差除以组内方差计算“separation”。因此,这个通过V2计算的这个间隔是:
calcBetweenGroupsVariance(wine[2],wine[1])/calcWithinGroupsVariance(wine[2],wine[1])
# 如果我们想通过多元统计数据的所有变量计算出间隔,你可以使用下述的“calcSeparations()”:
calcSeparations<-function(variables,groupvariable){
  # find out how many variables we have
  variables<-as.data.frame(variables)
  numvariables<-length(variables)
  # find the variable names
  variablenames<-colnames(variables)
  # calculate the separation for each variable
  for(i in 1:numvariables){
    variablei<-variables[i]
    variablename<-variablenames[i]
    Vw<-calcWithinGroupsVariance(variablei,groupvariable)
    Vb<-calcBetweenGroupsVariance(variablei,groupvariable)
    sep<-Vb/Vw
    print(paste("variable",variablename,"Vw=",Vw,"Vb=",Vb,"separation=",sep))
  }
}
# 例如,计算每一个变量的13种化学物质浓度的间隔,我们输入:
calcSeparations(wine[2:14],wine[1])
# 因此,个体变量在组内(葡萄酒品种)的最大间隔是V2(间隔为233.0)。
# 正如我们将在下面讨论的,线性判别分析(LDA)的目的是寻找一个个体变量的线性组合将令组内(这里是品种)实现最大的间隔。
# 这里希望能够通过任何个体变量(暂时是V8的233.9)得到一个更好的间隔替代这个最优间隔。

相关内容

热门资讯

邓煜,诗人,数学家,老二次元 来源 视觉中国菲尔兹奖的通知邮件抵达时,普林斯顿大学数学博士、芝加哥大学数学系教授邓煜正在看一本恋爱...
安全生产许可证状态不是“有效”... 工程公司的安全生产许可证在官方系统显示为“其他”状态,有一栏则显示“整改”,却成为高标准农田项目的第...
深化产教融合 推进数智育人 哈... 7月23日,由阿里国际人工智能人才孵化中心(以下简称“阿里国际AITIC”)主办的“智启未来·数智赋...
中国工程院举办学习陈立泉、贲德... 北京7月24日电 (记者 孙自法)中国工程院7月24日在北京举办学习陈立泉、贲德院士科学家精神座谈会...
从成都看低空经济,专访杨军:万... 电子科技大学教授、深思实验室主任杨军接受封面新闻记者专访 封面新闻记者 王一理 摄影 陈光旭 近年来...
蚂蚁集团副总裁张俊杰:希望与成... 7月24日下午,APEC人工智能高级别论坛在成都举行。会议期间,蚂蚁集团副总裁、健康事业群总裁张俊杰...
北大博雅塔为王虹邓煜亮灯 极目新闻记者 陈洋洋7月24日晚,多位北京网友发帖称,在北京大学校园内的博雅塔上,看到了庆祝中国数学...
要和马六甲争高下的陆桥黄了,暴... (文/观察者网 郭光昊、张菁娟)绕开马六甲海峡,在泰国南部的狭窄地峡上“劈开一个口子”。这是每个中国...
AI做科研,走过概念期 2024年,诺贝尔化学奖颁给了谷歌DeepMind的两位人工智能科学家德米斯·哈萨比斯和约翰·杰珀,...
FLUX 3多模态AI模型登场... IT之家 7 月 24 日消息,Black Forest Labs 昨日(7 月 23 日)发布博文...