Rでデータ解析 その1
Rでデータ解析を行う。
> flour <- c(3,-2,-1,8,1,-2)
> diet <- c(-4,1,-3,-5,-2,-8)
> total<-c(flour, diet)
# histogramを書く
> hist(total)
![](https://assets.st-note.com/production/uploads/images/1653724/picture_pc_155a82520e45517387e40046339a5114.jpg)
> hist(total, br=c(-8,-6, -4, -2,0,2,4,6,8), ylim =c(0,5))
![](https://assets.st-note.com/production/uploads/images/1653726/picture_pc_364dfaf6350819f6df41136a54dcf5c1.jpg)
# 密度推定曲線
> plot(density(total), xlim=c(-8,8), ylim =c(0,0.2))
![](https://assets.st-note.com/production/uploads/images/1653729/picture_pc_cc9858c2cd0e45e32486ae490a841873.jpg)
> plot(density(flour), xlim=c(-8,8), ylim =c(0,0.2), lty=1)
![](https://assets.st-note.com/production/uploads/images/1653737/picture_pc_648216c3f9efe4b1780251c2b7f127e9.jpg)
> par(new=T)
> plot(density(diet), xlim=c(-8,8), ylim =c(0,0.2), lty=2)
![](https://assets.st-note.com/production/uploads/images/1653741/picture_pc_94be33a078bef6435d309e89d9d4807e.jpg?width=1200)
> legend(4,0.2,c("flour", "diet"), lty=1:2, ncol=1)
![](https://assets.st-note.com/production/uploads/images/1653744/picture_pc_78c5d2c49f62cc15b5a5d1c4c00f0ebc.jpg?width=1200)
# 箱ひげグラフ
> boxplot(flour, diet, names=c("flour", "diet"))
![](https://assets.st-note.com/production/uploads/images/1653747/picture_pc_8858bdfde914e7cfaf30c03b45addd45.jpg)
> boxplot(total, flour, diet, names=c("total", "flour", "diet"))
![](https://assets.st-note.com/production/uploads/images/1653749/picture_pc_0cf12fbdb2772fb3a5568d5129a7b35b.jpg)
# 合計
> sum(total)
[1] -14
> quantile(total)
0% 25% 50% 75% 100%
-8.00 -3.25 -2.00 1.00 8.00
# min, 1Q median, 3Q, max
> fivenum(total)
[1] -8.0 -3.5 -2.0 1.0 8.0
# 平方和
> sum((total-mean(total))^2)
[1] 185.6667
> total
[1] 3 -2 -1 8 1 -2 -4 1 -3 -5 -2 -8
#計算
> 3-2-1+8+1-2-4+1-3-5-2-8
[1] -14
# 標本分散
> variance <- function(x)var(x)*(length(x)-1)/length(x)
> variance(total)
[1] 15.47222
# 不偏分散
> var(total)
[1] 16.87879
# 標準誤差
> sd(total)
[1] 4.10838
> sqrt(var(total))
[1] 4.10838
# 不偏共分散行列
> var(flour, diet)
[1] -2.5
# 相関係数
> cor(flour, diet)
[1] -0.2142184
この記事が気に入ったらサポートをしてみませんか?