> st <- c(75,64,45.6,66.9,56.7,66,78.9,66.7,99.6,69)
> mean(st)
[1] 68.84
> hist(st)
> y<-desity(sy)
Error in desity(sy) : 没有"desity"这个函数
> y<-desity(st)
Error in desity(st) : 没有"desity"这个函数
> y<-density(st)
> lines(y) # 不显示是因为是以频率显示,改为以概率显示
> hist(st,freq=F)
> lines(y)
> N=1000
> mean=100
> sd=4
> x=rnorm(N,mean,sd)
> hist(x)
> # 以上的代码绘出直方图
>
> #hist(x,freq = NULL,probability = !freq,col=NULL,main=paste("Histogram of",xname),xlim=range(breaks),ylim=NULL,xlab=xname,ylab,axes=TRUE,nclass=NULL)
> # freq = NULL 纵坐标选频数
>
> N <-rnorm(1000,mean=100,sd=4)
> hist(N)
>
> # 核密度估计
> hist(N,col="blue",freq=False)
Error in hist.default(N, col = "blue", freq = False) : 找不到对象'False'
> hist(N,col="blue",freq=F)
> lines(density(x),col="green",lwd=3)
> # 在原有的直方图上加宽度为3的数据。线是利用核密度估计的数值。默认正态和函数
> N <- rbinom(1000,100,0.9) # 产生数据
> hist(N,col="lightgreen",freq=F)
> lines(density(N),col="yellow",lwd=3)
st <- c(75,64,45.6,66.9,56.7,66,78.9,66.7,99.6,69)
> mean(st)
[1] 68.84
> hist(st)
> y<-desity(sy)
Error in desity(sy) : 没有"desity"这个函数
> y<-desity(st)
Error in desity(st) : 没有"desity"这个函数
> y<-density(st)
> lines(y) # 不显示是因为是以频率显示,改为以概率显示
> hist(st,freq=F)
> lines(y)
> x<-45:75
> z <- dnorm(x,mean(st),sd(st),col="lightblue")
Error in dnorm(x, mean(st), sd(st), col = "lightblue") :
参数没有用(col = "lightblue")
> z <- dnorm(x,mean(st),sd(st))
> z
[1] 0.006856625 0.007699074 0.008602214 0.009563695 0.010579981 0.011646295
[7] 0.012756583 0.013903515 0.015078516 0.016271825 0.017472603 0.018669070
[13] 0.019848672 0.020998289 0.022104468 0.023153674 0.024132565 0.025028264
[19] 0.025828649 0.026522615 0.027100337 0.027553497 0.027875488 0.028061567
[25] 0.028108979 0.028017018 0.027787050 0.027422477 0.026928652 0.026312751
[31] 0.025583596
> lines(x,z,col="green")
> # 绿z为正态分布,黑x为核密度
>
> stem(st)
The decimal point is 1 digit(s) to the right of the |
4 | 67
6 | 4677959
8 |
10 | 0
> boxplot(st)
> PCA <- read.csv("C:/Users/Public/Desktop/EduRecivedFiles/PCA.student.csv",header=T)
> PCA
M P Chem C H E
1 65 61 72 84 81 79
2 77 77 76 64 70 55
3 67 63 49 65 67 57
4 80 69 75 74 74 63
5 74 70 80 84 81 74
6 78 84 75 62 71 64
7 66 71 67 52 65 57
8 77 71 57 72 86 71
9 83 100 79 41 67 50
> # 直方图
> hist(PCA$M,col="green")
> boxplot(PCA$M,col="blue")
> # 箱线图
> summary(PCA)
错误: unexpected input在"summary("里
> summary(PCA)
M P Chem C H
Min. :65.00 Min. : 61 Min. :49 Min. :41.00 Min. :65.00
1st Qu.:67.00 1st Qu.: 69 1st Qu.:67 1st Qu.:62.00 1st Qu.:67.00
Median :77.00 Median : 71 Median :75 Median :65.00 Median :71.00
Mean :74.11 Mean : 74 Mean :70 Mean :66.44 Mean :73.56
3rd Qu.:78.00 3rd Qu.: 77 3rd Qu.:76 3rd Qu.:74.00 3rd Qu.:81.00
Max. :83.00 Max. :100 Max. :80 Max. :84.00 Max. :86.00
E
Min. :50.00
1st Qu.:57.00
Median :63.00
Mean :63.33
3rd Qu.:71.00
Max. :79.00
PCA这个dataframe中提取math的成绩也可以用如下方式
> math <- PCA$m
> math
NULL
> math <- PCA$M
> math
[1] 65 77 67 80 74 78 66 77 83
> hist(math)
> # 柱形图
> barplot(table(PCA$M),col=c("blue","green")
+ )
> boxplot(PCA)
> boxplot(PCA[1:2])
> # 饼图
> pie(table(PCA$M,col=c("grey","yellow"))
+ )
Error in table(PCA$M, col = c("grey", "yellow")) : 所有参数的长度都应一致
> length(M)
错误: 找不到对象'M'
> length(PCA$M)
[1] 9
> table(PCA$M)
65 66 67 74 77 78 80 83
1 1 1 1 2 1 1 1
> # 多维列联表
> math
[1] 65 77 67 80 74 78 66 77 83
> # 接下来对math进行分段
> math <- cut(math,c(60,70,max(math)))
> math
[1] (60,70] (70,83] (60,70] (70,83] (70,83] (70,83] (60,70] (70,83] (70,83]
Levels: (60,70] (70,83]
> levels(math)=c("not bad","perfect")
> # cut(data,breaks)
> # levels(data) =c(names)
> table(math)
math
not bad perfect
3 6
> physics <- PCA$P
> physics
[1] 61 77 63 69 70 84 71 71 100
> physics <- cut(physics,c(60,70,max(physics)))
> levels(physics)=c("not bad","perfect")
> # 接下是二维列联表
> table(math,physics)
错误: unexpected input在"table(math,"里
> table(math,physics)
physics
math not bad perfect
not bad 2 1
perfect 2 4
> # 多维列联表的条形图
> barplot(table(math,physics),beside=T)
> boxplot(physics~math)
> boxplot(PCA$M~PCA$P)
>
> # 条件散点图
> plot(PCA$Chem,PCA$P)
> # 通过数学对C和P进行划分
> coplot(PCA$P~PCA$Chem|math)
> # 对应关系:coplot(y~x|z)
> # ,的对应关系:(x,y)
> 对应关系对应横纵坐标,y一般是纵坐标
> # (暴力做法)矩阵散点图
> pairs(PCA)
)
>)
>> pairs(PCA[c(1,3,5)])