R は統計計算とグラフィックスのためのフリーソフト(GNU-style copyleft)である。 Rは商用ソフトのS(AT&T ベル研究所のRichard A. Becker, John M. Chambers, and Allan R. Wilks により作られた統計解析やグラフィックスのための言語であり、製品としては S version 4 や S-Plusがある)に操作環境などが良く似ており、Sを使っている場合には関数の利用法がほぼ同じことから取っつきやすく、Sで定義した関数などはRでもほとんど変更なしに動作する。
Sの現在のバージョンほどは新たな手法やグラフ表示法は取り入れられていないが、通常の解析ならば不都合は感じないだろう。 CRAN(The Comprehensive R Archive Network)にはRに対する追加パッケージも沢山あり、新たな手法は、これらのパッケージを追加することにより解決する場合が多々ある。 RはSのクローンのフリーウェアと捉えられがちだが、全くのクローンではなく(もちろん完全なクローンを目指しているわけでもない)、内部的な構成などはSとは全く異なる。 詳細は R FAQの 3.3 What Are the Differences between R and S? に記載されている。
Rの利用方法はNotes on Rというドキュメントにまとめられていますが、山本和彦氏や東工大の間瀬氏が中心となり翻訳プロジェクト(ホームページでの呼びかけで集まった有志による)が組まれ、日本語版として利用できる。
Rにも非常によい入門用のテキストNotes on Rがインターネットで公開されているので、それをテキストとすれば習熟度はおのずと上がるだろう。R-jp プロジェクトによりNotes on Rは日本語訳が利用できる。RはS言語と互換があるので、Sのテキストも参考になるだろう。
起動後の R システムでは、> (プロンプト)が命令を待っている。ここに、式を入力すれば、その式を評価した結果が返る。また、単に数値を入力すると、その数値を表示する。そして、次のコマンドを待つプロンプトを表示する。終了は 命令
> q() |
> mean(c(148, 160, 159, 153, 151, 140, 158, 137, 149, 160)) [1] 151.5 |
> height <- c(148, 160, 159, 153, 151, 140, 158, 137, 149, 160)
> weight <- c(41, 49, 45, 43, 42, 29, 49, 31, 47, 47)
> height
[1] 148 160 159 153 151 140 158 137 149 160
> weight
[1] 41 49 45 43 42 29 49 31 47 47
> mean(height)
[1] 151.5
> c("x1", "x2", "x3")-> xname
|
> objects ()
[1] "height" "weight" "xname"
> objects (pat="we*")
[1] "weight"
> rm("xname")
> objects ()
[1] "height" "weight"
|
> sink("yama.log")
> objects()
> table(height)
> mean(height)
> sink()
|
> help("write")
> help.start()
updating HTML package listing
updating HTML search index
If nothing happens, you should open `C:\PROGRA~1\R\rw2001\doc\html\rwin.html ' yourself
> apropos("norm")
[1] "dlnorm" "dnorm" "plnorm" "pnorm" "qlnorm" "qnorm" "qqnorm" "rlnorm" "rnorm"
|
--height(身長)-- 148 160 159 153 151 140 158 137 149 160 151 157 157 144 139 139 149 142 150 139 161 140 152 145 156 147 147 151 141 148 --weight(体重)-- 41 49 45 43 42 29 49 31 47 47 42 39 48 36 32 34 36 31 43 31 47 33 35 35 44 38 30 36 30 38 |
148 41 160 49 159 45 % 中略 141 30 148 38 |
> in1 <- scan("sample1.dat")
Read 60 items
> in1
[1] 148 41 160 49 159 45 153 43 151 42 140 29 158 49 137 31 149 47 160
[20] 47 151 42 157 39 157 48 144 36 139 32 139 34 149 36 142 31 150 43
[39] 139 31 161 47 140 33 152 35 145 35 156 44 147 38 147 30 151 36 141
[58] 30 148 38
|
> in1 <- scan("sample1.dat", list(0,0))
Read 30 lines
> in1
[[1]]
[1] 148 160 159 153 151 140 158 137 149 160 151 157 157 144 139 139 149 142 150
[20] 139 161 140 152 145 156 147 147 151 141 148
[[2]]
[1] 41 49 45 43 42 29 49 31 47 47 42 39 48 36 32 34 36 31 43 31 47 33 35 35 44
[26] 38 30 36 30 38
|
> height <- in1[[1]] > weight <- in1[[2]] |
> in2 <- matrix(scan("sample1.dat"),ncol=2, byrow=T)
Read 60 items
> in2
[,1] [,2]
[1,] 148 41
[2,] 160 49
[3,] 159 45
[4,] 153 43
[5,] 151 42
(中略)
[29,] 141 30
[30,] 148 38
|
> in2[[1]] [1] 148 > in2[,1] [1] 148 160 159 153 151 140 158 137 149 160 151 157 157 144 139 139 149 142 150 [20] 139 161 140 152 145 156 147 147 151 141 148 > in2[1,] [1] 148 41 > in2[[1,2]] [1] 41 > apply(in2,2,mean) [1] 171.4 61.6 |
> mean(height) [1] 149 > var(height) [1] 53.51724 > sd(height) [1] 7.315548 |
> sex <- c(rep("M", time=16),rep("F",time=14))
> sex
[1] "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "F" "F" "F"
[20] "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
|
> tapply(height,sex,mean)
F M
147.7143 150.1250
> tapply(weight,sex,range)
$F
[1] 30 47
$M
[1] 29 49
|
> table(sex)
sex
F M
14 16
> table(height)
> table(height)
height
137 139 140 141 142 144 145 147 148 149 150 151 152 153 156 157 158 159 160 161
1 3 2 1 1 1 1 2 2 2 1 3 1 1 1 2 1 1 2 1
> table(cut(height,seq(135,165,by=5)))
(135,140] (140,145] (145,150] (150,155] (155,160] (160,165]
6 4 7 5 7 1
|
> sexf <- factor(sex) > sex [1] "M" "M" "M" "M" "M" "M" "F" "F" "F" "F" > sexf [1] M M M M M M F F F F Levels: F M > |
> phys.df <-data.frame(pheight=height,pweight=weight,psex=sexf) > phys.df pheight pweight psex 1 148 41 M 2 160 49 M 3 159 45 M 4 153 43 M 5 151 42 M 中略 29 141 30 F 30 148 38 F |
> pheight Error: Object "pheight" not found > phys.df$pheight [1] 148 160 159 153 151 140 158 137 149 160 151 157 157 144 139 139 149 142 150 [20] 139 161 140 152 145 156 147 147 151 141 148 |
> summary(phys.df)
pheight pweight psex
Min. :137.0 Min. :29.00 F:14
1st Qu.:142.5 1st Qu.:33.25 M:16
Median :149.0 Median :38.00
Mean :149.0 Mean :38.70
3rd Qu.:155.3 3rd Qu.:43.75
Max. :161.0 Max. :49.00
|
> pheight Error: Object "pheight" not found > attach(phys.df) > pheight [1] 148 160 159 153 151 140 158 137 149 160 151 157 157 144 139 139 149 142 150 [20] 139 161 140 152 145 156 147 147 151 141 148 > detach(phys.df) > pheight Error: Object "pheight" not found |
height,weight,sex 148,41,M 160,49,M 159,45,M 153,43,M % 中略 141,30,F 148,38,F |
> phys<- read.table("sample1.csv",header=T, sep=",")
> phys
height weight sex
1 148 41 M
2 160 49 M
3 159 45 M
4 153 43 M
5 151 42 M
6 140 29 M
中略
29 141 30 F
30 148 38 F
|
> demo(graphics) |
> hist(height) > hist(height,breaks=3) > hist(height,br=c(136,140,144,148,152,156,160,170)) |
> op <- par(mfrow=c(1,2)) #複数グラフモード、opにグラフパラメータを保存 > hist(height) > hist(weight) > hist(height, br=5,col="lightblue", border="pink") > par(op) #複数グラフモードの終了 > str(hist(height,plot=F)) List of 4 $ breaks : num [1:7] 135 140 145 150 155 160 165 $ counts : int [1:6] 6 4 7 5 7 1 $ intensities: num [1:6] 0.0400 0.0267 0.0467 0.0333 0.0467 ... $ mids : num [1:6] 138 143 148 153 158 ... > h <- hist(height,plot=F) > h$counts [1] 6 4 7 5 7 1 |
> boxplot(height)
> boxplot(height[(sex=="M")],height[(sex=="F")],names.x=c("Male","Female"))
|
plot() は引数として与えるデータにより、インデックスプロットや散布図などのx-yプロットをはじめ様々なグラフが作成できる。
> plot(height) > plot(seq(1,length(height)),height,type="b",xlab="") #上と同じインデックスプロット > plot(height,weight) #散布図 |
> boxplot(pheight~psex, data=phys.df)
> plot(sexf)
> plot(sex) #因子でなく文字列のリストなので
Error in plot.window(xlim, ylim, log, asp, ...) :
need finite ylim values
In addition: Warning messages:
1: NAs introduced by coercion
2: no finite arguments to min; returning Inf
3: no finite arguments to max; returning -Inf
> plot(phys.df) #データフレームをプロットすると
> plot(pweight~pheight, data=phys.df)
|
var
function (x, y = x, na.rm = FALSE, use)
{
if (missing(use))
use <- if (na.rm)
"complete.obs"
else "all.obs"
cov(x, y, use = use)
}
|
tmean <- function (x)
{
(sum(x)-max(x)-min(x))/(length(x)-2)
}
|
tmean2 <- function (x) tsum <- sum(x)-max(x)-min(x) tlen <- length(x)-2 tsum/tlen } |
> bicycle <- read.table("bicycle.csv",header=T,sep=",")
> bicycle
travel separation
1 12.8 5.5
2 12.9 6.2
3 12.9 6.3
4 13.6 7.0
5 14.5 7.8
6 14.6 8.3
7 15.1 7.1
8 17.5 10.0
9 19.5 10.8
10 20.8 11.0
> lm(separation ~ travel, data=bicycle)
Call:
lm(formula = separation ~ travel, data = bicycle)
Coefficients:
(Intercept) travel
-2.1825 0.6603
|
> bicycle.lm <- lm(separation ~ travel, data=bicycle)
> summary(bicycle.lm)
Call:
lm(formula = separation ~ travel, data = bicycle)
Residuals:
Min 1Q Median 3Q Max
-0.76990 -0.44846 0.03493 0.35609 0.84148
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.18247 1.05669 -2.065 0.0727 .
travel 0.66034 0.06748 9.786 9.97e-06 ***
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
Residual standard error: 0.5821 on 8 degrees of freedom
Multiple R-Squared: 0.9229, Adjusted R-squared: 0.9133
F-statistic: 95.76 on 1 and 8 degrees of freedom, p-value: 9.975e-006
> coef(bicycle.lm)
(Intercept) travel
-2.1824715 0.6603419
|
> names(bicycle.lm)
[1] "coefficients" "residuals" "effects" "rank"
[5] "fitted.values" "assign" "qr" "df.residual"
[9] "xlevels" "call" "terms" "model"
> bic.lm.summary<-summary(bicycle.lm)
> names(bic.lm.summary)
[1] "call" "terms" "residuals" "coefficients"
[5] "sigma" "df" "r.squared" "adj.r.squared"
[9] "fstatistic" "cov.unscaled"
> bic.lm.summary$residuals
1 2 3 4 5 6
-0.76990432 -0.13593851 -0.03593851 0.20182219 0.40751451 0.84148033
7 8 9 10
-0.68869060 0.62648893 0.10580520 -0.55263922
> resid(bicycle.lm)
1 2 3 4 5 6
-0.76990432 -0.13593851 -0.03593851 0.20182219 0.40751451 0.84148033
7 8 9 10
-0.68869060 0.62648893 0.10580520 -0.55263922
|
> plot(resid(bicycle.lm)) > qqnorm(resid(bicycle.lm)) |
> bicycle.lm2 <- update(bicycle.lm, . ~ .+I(travel^2))
> summary(bicycle.lm2)
Call:
lm(formula = separation ~ travel + I(travel^2), data = bicycle)
Residuals:
Min 1Q Median 3Q Max
-1.04030 -0.03974 0.14687 0.20731 0.58317
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -16.41924 7.84827 -2.092 0.0747 .
travel 2.43267 0.97196 2.503 0.0408 *
I(travel^2) -0.05339 0.02923 -1.827 0.1105
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
Residual standard error: 0.5121 on 7 degrees of freedom
Multiple R-Squared: 0.9478, Adjusted R-squared: 0.9329
F-statistic: 63.54 on 2 and 7 degrees of freedom, p-value: 3.251e-005
>
|