R 语言的探索性分析功能9.25

一、常用统计函数

• max(x) 返回向量x中最大的元素
• min(x) 返回向量x中最小的元素
• which.max(x) 返回向量x中最大元素的下标
• which.min(x) 返回向量x中最小元素的下标
• mean(x) 计算样本(向量)x的均值
• median(x) 计算样本(向量)x的中位数
• mad(x) 计算中位绝对离差

• var(x) 计算样本(向量)x的方差
• sd(x) 计算向量x的标准差
• rang(x) 返回长度为2的向量: c(min(x), max(x))
• IQR(x) 计算样本的四分位数极差
• quantile(x) 计算样本常用的分位数
• summary(x) 计算常用的描述性统计量
• mad(x) 计算中位绝对离差
• cumsum(x) 返回向量x的累积和
• cumprod(x) 返回向量x的累积积
• cummin(x) 返回向量x和累积最小值
• cummax(x) 返回向量x和累积最大值

• var(x,y) 计算样本(向量)x与y的方差
• cov(x,y) 计算样本(向量)x与y的协方差
• cor(x,y) 计算样本(向量)x与y的相关系数
• sort(x) 将向量x按升序排序, 选项decreasing=TRUE表示降
序
• order(x) 返回x的秩(升序)
• rank(x) 返回x的秩

二、基本统计功能

1、抽样函数
sample(1:52, 4)
#不放回随机抽样
sample(1:6,10,replace=TRUE)
#有放回的随机抽样
2、概率分布

密度 d 分布 p 分位数 q 随机数 r

R用不同的名字前缀表示不同的含义
• d表示概率密度函数
• p 表示累积分布函数(cumulative distribution function,
CDF)
• q 表示分位函数
• r 表示随机模拟(random deviates)或者随机数发生器
R内嵌的分布函数

 二项分布
p<-0.2
k<-seq(0,20)
plot(k,dbinom(k,20,p),
type='h', 
main='Binomial 
distribution, n=20, p=0.2',xlab='k')

 泊松分布
lambda<-4.0
k<-seq(0,20)
plot(k,dpois(k,lambda),type='h',main='Poisson distribution, lambda=5.5',xlab='k')

 

 正态分布

其中 add=T是在原图上增加的意思,lwd是线的宽度,lty是线的类型,legend函数进行图例说明。

curve(dnorm(x,0,1), xlim=c(-5,5), ylim=c(0,.8),col='red', lwd=2, lty=3)
curve(dnorm(x,0,2), add=T, col='blue', lwd=2, lty=2)
curve(dnorm(x,0,1/2), add=T, lwd=2, lty=1)
title(main="Gaussian distributions")
legend(par('usr')[2], par('usr')[4],xjust=1, c('sigma=1', 'sigma=2', 'sigma=1/2'),lwd=c(2,2,2),lty=c(3,2,1),col=c('red', 'blue', par("fg")))
#要注意的是,rnorm和dnorm其中的参数,分别是,数量,均值,标准差

fivenum函数(五数概要)

其中,attach函数直接加载某个数据集,使得可以直接取到该数据集的变量,而无需使用美元符号取出。

attach(faithful) 
> summary(eruptions)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.600   2.163   4.000   3.488   4.454   5.100 
> fivenum(eruptions)
[1] 1.6000 2.1585 4.0000 4.4585 5.1000

三、单变量绘图

 茎叶图(stem)
stem(eruptions)

  The decimal point is 1 digit(s) to the left of the |

  16 | 070355555588
  18 | 000022233333335577777777888822335777888
  20 | 00002223378800035778
  22 | 0002335578023578
  24 | 00228
  26 | 23
  28 | 080
  30 | 7
  32 | 2337
  34 | 250077
  36 | 0000823577
  38 | 2333335582225577
  40 | 0000003357788888002233555577778
  42 | 03335555778800233333555577778
  44 | 02222335557780000000023333357778888
  46 | 0000233357700000023578
  48 | 00000022335800333
  50 | 0370
直方图
hist(eruptions)

核密度曲线

rug绘制的是下方的须线

hist(eruptions, probability=TRUE)#注意probability=TRUE否则是无法绘制出来的
lines(density(eruptions,bw=0.1))
rug(eruptions)

经验累积分布
plot(ecdf(eruptions),do.points=FALSE,verticals=TRUE)
plot(ecdf(eruptions),do.points=TRUE,verticals=TRUE)

箱线图
boxplot(eruptions)

正态性检验-QQ图
qqnorm(eruptions,main="Normality Check via QQ Plot") #实际
qqline(eruptions, col='red') #模拟

 四、单变量的描述性统计

概要:summary、分位数:quantile、中位数:median、均值:mean、五分概要:fivenum、

四分位极值函数 ( IQR ( ))、标准差函数(sd( ))、 方差 函数var( )、 绝对离差函数mad()
样本偏度系数:skewness()、样本峰度系数:kurtosis()
正态分布
long <- eruptions[eruptions > 3] #选取出喷发时间大于3min的数据
> plot(ecdf(long), do.points=FALSE, 
+      verticals=TRUE) #绘制经验累积密度分布图
> x <- seq(3, 5.4, 0.01)
> lines(x, pnorm(x, mean=mean(long), 
+                sd=sqrt(var(long))), lty=3) #叠加正态分布图,需要的有mean和sd
和绝对离差函数(mad())来表示.

在一个经验累积密度图上叠加正态分布图。

 分位数比较图(QQ图)
 qqnorm(long); qqline(long)

Shapiro-Wilk 检验(W检验)
shapiro.test(long)
Kolmogorov-Smirnov检验
ks.test(long, "pnorm", mean = mean(long), sd = 
sqrt(var(long)))

五、双变量绘图函数

散点图
plot(college$Retention,college$Grad.rate)
scatter.smooth(college$Retention,college$Grad.rate)

第一句语句:绘制散点图、第二局语句:为散点绘制平滑的曲线scatter.smooth()

 含有局部拟合平滑曲线的散点图
 scatter.smooth(Pct.20,Pct.50)

残差相关图、预测值和残差关系图、箱线图、柱状图和点状图、

线状图
plot(pressure$temperature, pressure$pressure, type = "l");points(pressure$temperature, 
+          pressure$pressure,col="red")

points函数,在已有的图上,增加点; 

 线状图-时间序列、心形线

六、多变量绘图

plot(CO2)

 

例杜鹃把蛋下在其它种类鸟的鸟巢中, 这些鸟会帮它们孵化, 我们希望了解在不同类的鸟巢中杜鹃蛋的长度
library(DAAG)
> coplot(length~breadth|species,data=cuckoos)


library(ggplot2)
> ggplot(data=cuckoos)+
+     geom_point(mapping = aes(x=breadth,y=length))+
+     facet_wrap(~species)

 

 hisyogram函数是在lattice包里的;

library(lattice)
> histogram(~length|species,data=cuckoos)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值