理学療法士がまとめたノート

統計学備忘録 since2016

Rを使って統計学を勉強するブログです

個人的によく使うグラフ

サンプル

a <- iris$Sepal.Length[51:75]
b <- iris$Petal.Length[101:125]
c <- a+b
data <- data.frame(a,b,c)

summary(data)
       a               b              c        
 Min.   :4.900   Min.   :4.50   Min.   :10.10  
 1st Qu.:5.600   1st Qu.:5.10   1st Qu.:11.10  
 Median :6.100   Median :5.60   Median :11.50  
 Mean   :6.012   Mean   :5.64   Mean   :11.65  
 3rd Qu.:6.400   3rd Qu.:6.00   3rd Qu.:12.30  
 Max.   :7.000   Max.   :6.90   Max.   :13.10  

ヒスト&箱ひげ&散布

row =3 
col =1
data <- a
par(mfrow=c(row,col)) 
hist(data)
boxplot(data,horizontal=T)
stripchart(data,vertical = F)
par(mfrow=c(1,1)) 

f:id:yoshida931:20190418235503p:plain:w300

グラフを並べる ヒスト&箱ひげ

Rの様々な種類のグラフでデータの分布を理解する(hist、boxplot、stripchart) - Rプログラミングの小ネタ

row =2 
col =3
par(mfrow=c(row,col)) 

for(i in 1:3){
  hist(data[,i],main=colnames(data)[i])#breaks=seq(2,6,3),2~6までを3つに分ける
}
for(i in 1:3){
  boxplot(data[,i],main=colnames(data)[i])#breaks=seq(2,6,3),2~6までを3つに分ける
}
par(mfrow=c(1,1)) 

f:id:yoshida931:20190418235833p:plain:w500

ヒストグラムの下に箱ひげ
RPubs - ggplot2でヒストグラムと箱ひげ図を同時に描く&amp;グラフを一括出力する

library(ggplot2)
library(gridExtra)
Hist1 <- ggplot(data, aes(x = c))+
  geom_histogram(binwidth = 0.6)

Box1 <- ggplot(data, aes(x = 1, y = c))+
  geom_boxplot()+
  coord_flip()  # グラフを横向きにする

grid.arrange(Hist1, Box1, nrow = 2,ncol = 1) 

f:id:yoshida931:20190419000043p:plain:w350

箱ひげ図に平均値と不偏分散の平方根

Hamada Akira | SLA Lab

means <- colMeans(data[,]);means
sds <- apply(data,2,sd);sds  #不偏分散の平方根
sd1 <- sqrt( sum("vector"^2) / length ("vector") - ( mean ("vector") ) ^2) #データの標準偏差
sd2 <- sqrt(var("vector")/length("vector"))#標準誤差

不偏分散の平方根を使用して描いてみます

x <- c (1, 2, 3)#x軸
boxplot(data)
points (        #箱ひげ+平均値        
  means, 
  pch = 19,
  col = "red"
)

arrows (     #箱ひげ+平均値+不偏分散の平方根
  x,means,
  x,means + sds,
  length = 0.1,
  angle = 45,
  col = "red"
)

arrows (
  x,means,
  x,means - sds, 
  length = 0.1, 
  angle = 45, 
  col = "red"
)

f:id:yoshida931:20190419000346p:plain:w400

箱ひげ図に平均値と標準誤差

boxplot(a,b)
se1 <- sqrt(var(a)/length(a))#標準誤差
se2 <- sqrt(var(b)/length(b))#標準誤差
se <- c(se1, se2)
x <- c (1, 2)#x軸

points (        #箱ひげ+平均値        
  means, 
  pch = 19,
  col = "red"
)
arrows (     #箱ひげ+平均値+不偏分散の平方根
  x,means,
  x,means + se,
  length = 0.1,
  angle = 45,
  col = "red"
)
arrows (
  x,means,
  x,means - se, 
  length = 0.1, 
  angle = 45, 
  col = "red"
)

f:id:yoshida931:20190419000538p:plain:w400

箱ひげ図に平均値と95%CI

da <- data$c
means <- mean(da)
tc <- t.test(da)
tc$conf.int[1]
tc$conf.int[2]
x <- 1
boxplot(da)
points (        #箱ひげ+平均値        
  means, 
  pch = 19,
  col = "red"
)

arrows (
  x,means,
  x,tc$conf.int[2] ,
  length = 0.1,
  angle = 90,
  col = "red"
)

arrows (
  x,means,
  x,tc$conf.int[1], 
  length = 0.1, 
  angle = 90, 
  col = "red"
)

f:id:yoshida931:20190419083232p:plain:w400

横にzだけ移動

means <- mean(da)
tc <- t.test(da)
tc$conf.int[1]
tc$conf.int[2]
x <- 1
boxplot(da)
z = 0.25
points ( x+z,       #箱ひげ+平均値        
  means, 
  pch = 19,
  col = "red"
)
arrows (
  x+z, means,
  x+z, tc$conf.int[2] ,
  length = 0.1,
  angle = 90,
  col = "red"
)
arrows (
  x+z, means,
  x+z, tc$conf.int[1], 
  length = 0.1, 
  angle = 90, 
  col = "red"
)

f:id:yoshida931:20190419084022p:plain:w400