Use R

Similar documents
(lm) lm AIC 2 / 1

講義のーと : データ解析のための統計モデリング. 第3回

1 15 R Part : website:

k2 ( :35 ) ( k2) (GLM) web web 1 :

DAA09

k3 ( :07 ) 2 (A) k = 1 (B) k = 7 y x x 1 (k2)?? x y (A) GLM (k

講義のーと : データ解析のための統計モデリング. 第5回

1 R Windows R 1.1 R The R project web R web Download [CRAN] CRAN Mirrors Japan Download and Install R [Windows 9

> usdata01 と打ち込んでエンター キーを押すと V1 V2 V : : : : のように表示され 読み込まれていることがわかる ここで V1, V2, V3 は R が列のデータに自 動的につけた変数名である ( variable

R John Fox R R R Console library(rcmdr) Rcmdr R GUI Windows R R SDI *1 R Console R 1 2 Windows XP Windows * 2 R R Console R ˆ R

一般化線形 (混合) モデル (2) - ロジスティック回帰と GLMM

kubostat2017c p (c) Poisson regression, a generalized linear model (GLM) : :

<4D F736F F F696E74202D BD95CF97CA89F090CD F6489F18B4195AA90CD816A>

インターネットを活用した経済分析 - フリーソフト Rを使おう

BMIdata.txt DT DT <- read.table("bmidata.txt") DT head(dt) names(dt) str(dt)

untitled

untitled

1 Stata SEM LightStone 3 2 SEM. 2., 2,. Alan C. Acock, Discovering Structural Equation Modeling Using Stata, Revised Edition, Stata Press.

1 Stata SEM LightStone 4 SEM 4.. Alan C. Acock, Discovering Structural Equation Modeling Using Stata, Revised Edition, Stata Press 3.

(2/24) : 1. R R R

J1順位と得点者数の関係分析

201711grade2.pdf

yamadaiR(cEFA).pdf

kubostat2015e p.2 how to specify Poisson regression model, a GLM GLM how to specify model, a GLM GLM logistic probability distribution Poisson distrib

kubostat2018d p.2 :? bod size x and fertilization f change seed number? : a statistical model for this example? i response variable seed number : { i

情報管理学科で学ぶ

kubostat2017e p.1 I 2017 (e) GLM logistic regression : : :02 1 N y count data or

,, Poisson 3 3. t t y,, y n Nµ, σ 2 y i µ + ɛ i ɛ i N0, σ 2 E[y i ] µ * i y i x i y i α + βx i + ɛ i ɛ i N0, σ 2, α, β *3 y i E[y i ] α + βx i

R Console >R ˆ 2 ˆ 2 ˆ Graphics Device 1 Rcmdr R Console R R Rcmdr Rcmdr Fox, 2007 Fox and Carvalho, 2012 R R 2

/ 60 : 1. GLM? 2. A: (pwer functin) x y?

σ t σ t σt nikkei HP nikkei4csv H R nikkei4<-readcsv("h:=y=ynikkei4csv",header=t) (1) nikkei header=t nikkei4csv 4 4 nikkei nikkei4<-dataframe(n

Microsoft Word - News 18 本文.doc

kubostat7f p GLM! logistic regression as usual? N? GLM GLM doesn t work! GLM!! probabilit distribution binomial distribution : : β + β x i link functi

日本内科学会雑誌第102巻第4号

回帰分析 単回帰

Rによる計量分析:データ解析と可視化 - 第3回 Rの基礎とデータ操作・管理

Debian での数学ことはじめ。 - gnuplot, Octave, R 入門

²¾ÁÛ¾õ¶·É¾²ÁË¡¤Î¤¿¤á¤Î¥Ñ¥Ã¥±¡¼¥¸DCchoice ¡Ê»ÃÄêÈÇ¡Ë

Microsoft Word - 計量研修テキスト_第5版).doc

最小2乗法

28

kubostat2017j p.2 CSV CSV (!) d2.csv d2.csv,, 286,0,A 85,0,B 378,1,A 148,1,B ( :27 ) 10/ 51 kubostat2017j (

R R-console R R Rscript R-console GUI 1

講義のーと : データ解析のための統計モデリング. 第2回

<4D F736F F F696E74202D2088E D8C768A7789C482CC8A778D5A F939D8C76835C FC96E52E >

GLM PROC GLM y = Xβ + ε y X β ε ε σ 2 E[ε] = 0 var[ε] = σ 2 I σ 2 0 σ 2 =... 0 σ 2 σ 2 I ε σ 2 y E[y] =Xβ var[y] =σ 2 I PROC GLM

Stata11 whitepapers mwp-037 regress - regress regress. regress mpg weight foreign Source SS df MS Number of obs = 74 F(

<4D F736F F D20939D8C7689F090CD985F93C18EEA8D758B E646F63>

R による共和分分析 1. 共和分分析を行う 1.1 パッケージ urca インスツールする 共和分分析をするために R のパッケージ urca をインスツールする パッケージとは通常の R には含まれていない 追加的な R のコマンドの集まりのようなものである R には追加的に 600 以上のパッ

60 (W30)? 1. ( ) 2. ( ) web site URL ( :41 ) 1/ 77

y i OLS [0, 1] OLS x i = (1, x 1,i,, x k,i ) β = (β 0, β 1,, β k ) G ( x i β) 1 G i 1 π i π i P {y i = 1 x i } = G (

と入力する すると最初の 25 行が表示される 1 行目は変数の名前であり 2 列目は企業番号 (1,,10),3 列目は西暦 (1935,,1954) を表している ( 他のパネルデータを分析する際もデ ータをこのように並べておかなくてはならない つまりまず i=1 を固定し i=1 の t に関

Isogai, T., Building a dynamic correlation network for fat-tailed financial asset returns, Applied Network Science (7):-24, 206,

lec03

2 / 39

第11回:線形回帰モデルのOLS推定

2 と入力すると以下のようになる > x1<-c(1.52,2,3.01,9,2,6.3,5,11.2) > y1<-c(4,0.21,-1.5,8,2,6,9.915,5.2) > cor(x1,y1) [1] > cor.test(x1,y1) Pearson's produ

p.1/22

4 OLS 4 OLS 4.1 nurseries dual c dual i = c + βnurseries i + ε i (1) 1. OLS Workfile Quick - Estimate Equation OK Equation specification dual c nurser

% 10%, 35%( 1029 ) p (a) 1 p 95% (b) 1 Std. Err. (c) p 40% 5% (d) p 1: STATA (1). prtesti One-sample test of pr

こんにちは由美子です

こんにちは由美子です

Microsoft Word - 計量研修テキスト_第5版).doc

10

.3 ˆβ1 = S, S ˆβ0 = ȳ ˆβ1 S = (β0 + β1i i) β0 β1 S = (i β0 β1i) = 0 β0 S = (i β0 β1i)i = 0 β1 β0, β1 ȳ β0 β1 = 0, (i ȳ β1(i ))i = 0 {(i ȳ)(i ) β1(i ))


untitled

( 28 ) ( ) ( ) 0 This note is c 2016, 2017 by Setsuo Taniguchi. It may be used for personal or classroom purposes, but not for commercial purp

日本内科学会雑誌第97巻第7号

日本内科学会雑誌第98巻第4号

Microsoft PowerPoint - Econometrics

1.2 R R Windows, Macintosh, Linux(Unix) Windows Mac R Linux redhat, debian, vinelinux ( ) RjpWiki ( RjpWiki Wiki

linguistics

1 環境統計学ぷらす 第 5 回 一般 ( 化 ) 線形混合モデル 高木俊 2013/11/21

(pdf) (cdf) Matlab χ ( ) F t

1 2 Windows 7 *3 Windows * 4 R R Console R R Console ˆ R GUI R R R *5 R 2 R R R 6.1 ˆ 2 ˆ 2 ˆ Graphics Device 1 Rcmdr R Console R Rconsole R --sdi R M

卒業論文

「スウェーデン企業におけるワーク・ライフ・バランス調査 」報告書

mosaic Daniel Kaplan * 1 Nicholas J. Horton * 2 Randall Pruim * 3 Macalester College Amherst College Calvin College St. Paul, MN Amherst, MA Grand Rap

untitled

1 kawaguchi p.1/81

Stata 11 Stata ROC whitepaper mwp anova/oneway 3 mwp-042 kwallis Kruskal Wallis 28 mwp-045 ranksum/median / 31 mwp-047 roctab/roccomp ROC 34 mwp-050 s

Microsoft Word - StatsDirectMA Web ver. 2.0.doc

以下の内容について説明する 1. VAR モデル推定する 2. VAR モデルを用いて予測する 3. グレンジャーの因果性を検定する 4. インパルス応答関数を描く 1. VAR モデルを推定する ここでは VAR(p) モデル : R による時系列分析の方法 2 y t = c + Φ 1 y t

kubostat2017b p.1 agenda I 2017 (b) probability distribution and maximum likelihood estimation :

漸化式のすべてのパターンを解説しましたー高校数学の達人・河見賢司のサイト

ECCS. ECCS,. ( 2. Mac Do-file Editor. Mac Do-file Editor Windows Do-file Editor Top Do-file e

橡表紙参照.PDF

今回 次回の要点 あぶない 時系列データ解析は やめましょう! 統計モデル のあてはめ Danger!! (危 1) 時系列データの GLM あてはめ (危 2) 時系列Yt 時系列 Xt 各時刻の個体数 気温 とか これは次回)

tokei01.dvi

q( ) 2: R 2 R R R R C:nProgram FilesnRnrw1030) [File] [Change Dir] c:ndatadir OK 2

10:30 12:00 P.G. vs vs vs 2

第86回日本感染症学会総会学術集会後抄録(I)

R-introduction.R

/ 55 2 : : (GLM) 1. 1/23 ( )? GLM? (GLM ) 2.! 1/25 ( ) ffset (GLM )

untitled

数理統計学Iノート

GDP

: (EQS) /EQUATIONS V1 = 30*V F1 + E1; V2 = 25*V *F1 + E2; V3 = 16*V *F1 + E3; V4 = 10*V F2 + E4; V5 = 19*V99

H22 BioS t (i) treat1 treat2 data d1; input patno treat1 treat2; cards; ; run; 1 (i) treat = 1 treat =

Transcription:

Use R! 2008/05/23( ) Index Introduction (GLM) ( ) R. Introduction R,, PLS,,, etc. 2. Correlation coefficient (Pearson s product moment correlation) r = Sxy Sxx Syy :, Sxy, Sxx= X, Syy Y 1.96 95% R

cor(x, Y, method = c( pearson )) #X, Y # R x <- rnorm(150) y <- rnorm(150) # 150 # x y plot(x, y) # # # cor(x, y, method="pearson") # R 0 p p p p cor.test(x, y, method= pearson ) # p Pearson's product-moment correlation data: x and y t = 2.7246, df = 148, p-value = 0.007214 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.06040149 0.36599028 sample estimates: cor 0.2185475 2 t-value df Degree of freedom ( 2) p-value( ) t-value t = r ( n 2) (1 r 2 ) t t-value, r n

95% 0 0 ( ) R # cor(x, y, method = spearman ) cor(x, y, method = kendall ) # # # p cor.test(x, y, method= spearman ) cor.test(x, y, method= kendall ) method= ( ) X, Y R ( ) ( ) ( ) R airquality airquality cor( airquality, method= pearson ) cor( airquality, method= pearson, use= pairwise ) NA

3. hist ( ) airquality 1973 5-7 airquality attach(airquality) names(airquality) R y β + α + ε ε y~x ~ 0 0 Y~X+0 # (Wind Ozone ) plot(wind, Ozone) # plot(x, Y ) result <- lm(ozone~wind) # result abline(result) # result # Call: lm(formula = Ozone ~ Wind) Coefficients: (Intercept) Wind # Ozone=-5.551Wind+96.873 96.873-5.551 Summary summary(result) Call: lm(formula = Ozone ~ Wind) Residuals: # ( ( ) 1(3) ) Min 1Q Median 3Q Max -51.572-18.854-4.868 15.234 90.000 Coefficients: # Estimate Std. Error t value Pr(> t ) (Intercept) 96.8729 7.2387 13.38 < 2e-16 *** # 0 Wind -5.5509 0.6904-8.04 9.27e-13 *** # 0 S ignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 26.47 on 114 degrees of freedom Multiple R-Squared: 0.3619, Adjusted R-squared: 0.3563 #R 2 ajsr 2 F-statistic: 64.64 on 1 and 114 DF, p-value: 9.272e-13 # p

new <- data.frame(wind = seq(min(wind), max(wind), by = 0.1)) # cline <-predict(result, new, interval="confidence") # cline plot(wind, Ozone, xlab = "Wind", ylab = "Ozone") # matplot(new, cline, lty=c(1,2,2), type="l", add=t) # ( ) (X) ( ) (x) Ozone Wind Ozone (NO) y x Wind Ozone Ozone Wind 2 (Major axis regression) 4. R 2 0.36 R 2 ( ) attach(airquality)

lm (Linear Model ) summary summary(result) Call: lm(formula = Ozone ~ Wind + Temp) Residuals: Min 1Q Median 3Q Max -41.251-13.695-2.856 11.390 100.367 Coefficients: Estimate Std. Error t value Pr(> t ) # (Intercept) -71.0332 23.5780-3.013 0.00320 ** Wind -3.0555 0.6633-4.607 1.08e-05 *** Temp 1.8402 0.2500 7.362 3.15e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 21.85 on 113 degrees of freedom Multiple R-Squared: 0.5687, Adjusted R-squared: 0.5611 F-statistic: 74.5 on 2 and 113 DF, p-value: < 2.2e-16 Ozone = 1.84 * Temp 3.06*Wind 71.0 R 2 =0.57 Multi-co linearity - - ( 0) Wind Temp 1 ( 0 ) 0

VIF ( Variance Inflation factor ) VIF 10 1 VIF = 1 2 R j Rj 2 ; Xj ( N-1 ) R DAAG VIF R DAAG # attach(airquality) result <- lm(ozone~wind+temp+solar.r) vif(result) # 3 # VIF Wind Temp Solar.R 1.3291 1.4314 1.0953 detach(airquality) #detach 10 VIF Ozone = α*wind + β*temp + γ + e Wind Temp Wind Temp Ozone = Wind + Temp + Wind Temp = (1 + Temp)Wind + Temp Wind Temp Wind Wind Temp Wind Temp

R attach(airquality) Wind2 <- (Wind ave(wind)) # ( Temp2 <- (Temp ave(temp)) result <- lm(ozone~wind+temp+wind2*temp2) summary(result) # Call: lm(formula = Ozone ~ Wind + Temp + Wind2 * Temp2) Coefficients: (2 not defined because of singularities) Estimate Std. Error t value Pr(> t ) (Intercept) -74.86780 22.06961-3.392 0.000959 *** Wind -3.10381 0.62038-5.003 2.11e-06 *** Temp 1.84614 0.23377 7.897 2.13e-12 *** Wind2 NA NA NA NA Temp2 NA NA NA NA Wind2:Temp2-0.22391 0.05399-4.147 6.57e-05 *** # --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 20.44 on 112 degrees of freedom Multiple R-Squared: 0.6261, Adjusted R-squared: 0.6161 F-statistic: 62.52 on 3 and 112 DF, p-value: < 2.2e-16 detach(airquality) 2 (R 2 ) 1.00 (Over fitting ) F AIC AIC AIC (GLM)

AIC(Akaike s Information Criteria) AIC = 2*p 2 ( 1/2 *n*log(2 2 )) p () 2 = n 2 ( e ) n i= 1 i / / AIC ( ) ( ) AIC p 2 P AIC wle AIC mle.aic HP AIC # Wind Temp 2 attach(airquality) Wind2 <- (Wind ave(wind)) # Temp2 <- (Temp ave(temp) ) WinTem <- (Wind2 * Temp2) # result <- lm(ozone~wind+temp+wintem) # select <- mle.aic(result) # select summary(select) # select Call: mle.aic(formula = Ozone ~ Wind + Temp + WinTem) Akaike Information Criterion (AIC): (Intercept) Wind Temp WinTem aic # (1) (0) [1,] 1 1 1 1 1033 [2,] 0 1 1 1 1043 [3,] 1 1 1 0 1048 [4,] 1 0 1 1 1056 [5,] 0 1 1 0 1057 [6,] 1 0 1 0 1071 [7,] 1 1 0 1 1094 [8,] 1 1 0 0 1108 [9,] 0 0 1 1 1147 [10,] 0 0 1 0 1156 --------------------------------------------------------------------- # Printed the first 15 best models AIC

5. (GLM, Generalized Linear Model) GLM lm glm lm GLM (Binomial) (Poisson) (Gamma) ( ) ( ) HP GLM(+GLMM) @ lm 6 nls Non-linear Least Square ( ) ( ) ( ) (GAM) ( ) nls nls lm 2 R nls SSlogis ( ) ( ( )) a f ( x i ) = + ε cx i i 1 + b * e

a, b, c: ( ) c >0, ε: 0 ( ) ( ) ( ) dy = dx α β ( y ) y β a= α/β, α/β β α y( ) α/β 0 y # X <- (1:20) Y <- c(3, 16, 54, 139, 263, 420, 611, 750, 860, 900, 940, 950, 960, 930, 980, 970, 989, 980, 975, 980) Y <- Y + rnorm(20, 40, 20) # plot(y~x) # plot(x, Y) # result <- nls (Y ~ a / (1+b*exp(-c*X)), start = c(a=1000, b=100, c=1), trace=t) summary(result) Formula: Y ~ a/(1 + b * exp(-c * X)) Parameters: Estimate Std. Error t value Pr(> t ) a 986.77806 4.18655 235.702 < 2e-16 *** b 103.95239 12.46723 8.338 2.07e-07 *** c 0.74022 0.01929 38.380 < 2e-16 *** # --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 12.75 on 17 degrees of freedom # AIC AIC(result) lines(x, fitted(result)) # (1000 a=1000 )

@ glm 0 X( ) X ( ) ( ) glm GLM GLMM GLM 7. R R help! help example( ) example( ) help HP PC 8. The R-tips,2005, 9-ten (Web ) Statistics an introduction using R M.J. Crawley, 2005, John Wiley & Sons,, HP http://phi.med.gunma-u.ac.jp/index.html # HP http://aoki2.si.gunma-u.ac.jp/ # HP http://www.is.titech.ac.jp/~shimo/index-j.html HP http://home.hiroshima-u.ac.jp/kazu711/stat/hp_mr_0.html

x y y= x= ( ) Smatr plot(airquality$wind, airquality$ozone, xlab = "Wind", ylab = "Ozone") result <- lm(airquality$ozone~airquality$wind) # abline(result) result2 <- lm(airquality$wind~airquality$ozone) # abline(-result2$coef[1]/result2$coef[2], 1/result2$coef[2], lty=2) # #SMA SMA <- f unction(x, y) { # 2 dt <- data.frame(x, y) dt2 <- na.omit(dt) x1 <- dt2$x y1 <- dt2$y slope <- sign(cor(x1, y1))*sqrt(var(y1)/v ar(x1)) # intercept <- mean(y1)-slope*mean(x1) # return(list(slope=slope, Intercept=intercept)) } result3 <- SMA(airquality$Wind, airquality$ozone) result3 abline(result3$intercept, result3$slope, col="red") # (SMA) # # # smart library(smatr) # res <- line.cis(airquality$ozone, airquality$wind) # (SMA) res # coef(sma) lower limit upper limit # 95 ( ) elevation 133.134048 118.590129 147.67797 slope -9.227753-7.960876-10.69624 # -5.55 slope.test(airquality$ozone, airquality$wind, test.value = -5.55) $r [1] 0.5532898 $p [1] 1.186690e-10 # $test.value [1] -5.55 #