またまたRの続きをやっています。

いよいよ最終章~というのに気づくのが遅れ、慌てて本を発注~
今度こそは21日を途切れさせないようにと思ったのに・・・

Rを使って実際に検定をしてみるという作業はなかなか楽しいです。


(1)パッケージのデータセットを使う(id・性別・肥満度・収縮期血圧)
> install.packages("ISwR",rep="http://cran.ism.ac.jp")
Installing package into ‘C:/Users/madoka/Documents/R/win-library/3.2’
(as ‘lib’ is unspecified)
trying URL 'http://cran.ism.ac.jp/bin/windows/contrib/3.2/ISwR_2.0-6.zip'
Content type 'application/zip' length 217390 bytes (212 KB)
downloaded 212 KB

package ‘ISwR’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\madoka\AppData\Local\Temp\RtmpcLNG5d\downloaded_packages

> library(ISwR)
> data(bp.obese)
> bp.obese
sex obese bp
1 0 1.31 130
2 0 1.31 148
3 0 1.19 146
4 0 1.11 122
5 0 1.34 140
6 0 1.17 146
    ・
    ・
    ・


(2)散布図を描画(女性のみ)
> woman=bp.obese[bp.obese$sex==1,]
> plot(woman$bp~woman$obese)




(3)womanをグローバル化
> attach(woman)


(4)回帰分析(女性のみ)
> lm.bp=lm(bp~obese)
> summary(lm.bp)

Call:
lm(formula = bp ~ obese)

Residuals:
Min 1Q Median 3Q Max
-23.522 -12.348 -2.203 3.907 71.500

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 82.517 12.048 6.849 6.14e-09 ***
obese 31.204 8.426 3.703 0.000488 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 17.56 on 56 degrees of freedom
Multiple R-squared: 0.1967, Adjusted R-squared: 0.1824
F-statistic: 13.72 on 1 and 56 DF, p-value: 0.000488

回帰式:血圧=82.517+31.204*肥満度


(5)回帰直線を散布図上に描く
> plot(bp~obese)
> abline(lm.bp,col="red")





演習問題として「男性の場合の単回帰モデルをあてはめなさい」というものが出ているので
続きを別記事にて行いたいと思います。