ãããã¨
ä¸è¬åç·å½¢æ··åã¢ãã«ã使ã£ãåä½å¥ã®ãã©ã¡ã¼ã¿ã®ç¸®å°å¹æã«ã¤ãã¦ããã¾ããã
éå»ã«æ¸ãã以ä¸ã®è¨äºã®ç¶ãã§ãã
githubã¯ä»¥ä¸ã
Rã®ãã¼ã¸ã§ã³
version
## _
## platform x86_64-pc-linux-gnu
## arch x86_64
## os linux-gnu
## system x86_64, linux-gnu
## status
## major 4
## minor 0.3
## year 2020
## month 10
## day 10
## svn rev 79318
## language R
## version.string R version 4.0.3 (2020-10-10)
## nickname Bunny-Wunnies Freak Out
ä¸è¬åç·å½¢æ··åã¢ãã«ã£ã¦ä½ï¼
è¦ã¯ãæã£ã¦ãããã¼ã¿ãã°ã«ã¼ãåãã§ããå ´åããã®ã°ã«ã¼ãå¥ã«èª¬æå¤æ°ã®å¹æãå¤ããããããªãï¼ã
ã¨ãã仮説ãç·åå帰ã«åæ ããã¢ãã«ã¨è§£éããã¾ãã
é£ããããã¨ãæ¨å®ããããã©ã¡ã¼ã¿ããåä½å¥ãã®å¹æã¨ãã°ã«ã¼ãå¥ãã®å¹æã®ï¼ã¤ã«åããããã
ã¨ããå½¢ã§è¡¨ç¾ãããã¨ããã¢ãã«ã«ãªãã¾ã(çäºã»ä¸ç ´ï¼2008)ã
åä½å¥ã®å¹æ(æ®éã®å帰åæã§ã®å帰ä¿æ°)ããåºå®å¹æããã°ã«ã¼ãå¥ã®å¹æããã©ã³ãã å¹æããããã¯ãå¤éå¹æãã¨å¼ãã ããã¾ãã
çè
ã¯æ²ãããªãã«ããã¼ã±ãã£ã³ã°æ¥çã«ããã¤ãããªã®ã§ãå¿ç¨å
ãèããã¨ã
åºèå¥ã®æ½çå¹æã®éãã®è©ä¾¡
å°åå¥ã®åºåå¹æã®éãã®è©ä¾¡
調æ»ã»ã°ã¡ã³ãã°ã«ã¼ãã«ããååè©ä¾¡ã®éãã®è©ä¾¡
ãªã©ãå¿ç¨å
ã¯åºããªãã¨æã£ã¦ãã¾ãã
ããããç®çã«ãã°ã«ã¼ãå¥ã«ç¬ç«ã«ãã©ã¡ã¼ã¿ãæ¨å®ãããã¨ããæ¹æ³ãããã¾ããã
ã°ã«ã¼ãåä½ã§ã®ã¬ã³ã¼ãæ°ãå°ãªãå ´åãæ¨å®ããããã©ã¡ã¼ã¿ã®åæ£ã大ãããªãã¨ããåé¡ãããã¾ã(ä¹
ä¿å·ï¼2006)ã
ããããå°å°ååé¡ãã¨ããã¾ããããããæã«ãå
¨ä½ã®å¹³åãæ¹åã«æ¨å®å¤ãã縮å°ããããã¨ã§ã
ãã©ã¡ã¼ã¿æ¨å®å¤ã®åæ£ãçããã縮å°æ¨å®ãã¨ããæ¹æ³ããããä»åã®ã¢ãã«ã¯ãã®æ¹æ³ã«åã£ã¦ãã¾ãã
ä¸è¬åç·å½¢æ··åã¢ãã«ã«ããæ¨å®å¤ã®ç¸®å°ã«ã¤ãã¦ã¯lme4
ã®è«æãªã©ã«ãè¨è¼ããã¦ãã¾ãã
å¤éå¹æãèæ
®ãããã¨ã§ãåºå®å¹æã¯å
¨ä½å¹³åæ¹åã«ç¸®å°ããã¦æ¨å®ããããã¨ããã話(ã®ã¯ã)ã§ãã
Rã«ããå®è£
ã«ã¯ããã¤ãæ¹æ³ããããlme4
ããã±ã¼ã¸ã使ãæ¹æ³ã¨ã
stan
ã«ããé層ãã¤ãºã¢ãã«ã«ãã£ã¦è¡¨ç¾ããæ¹æ³ãããã¾ãã
ãã ãlme4
ããã±ã¼ã¸ã§ã¯å帰ä¿æ°ã®æææ§æ¤å®ãè¨ç®ããã¾ããã
æææ§ã¯é·ãå帰ä¿æ°ãçµ±è¨çã«æå³ã®ããçµæãã©ããã示ãææ¨ã¨ãã¦ç¨ãããã¦ãã¾ãããã
2016å¹´ã«ASAãpå¤ã«å¯¾ãã声æãåºãã¦ãããã
ä¸è¬åç·å½¢æ··åã¢ãã«ã§ã¯ãã©ã¡ã¼ã¿èªç±åº¦ã®æ±ºå®ãé£ãã ã¨ãããã¨ããã£ããã¨ãåæ¹é¢ããã®çç±ãããã¾ãã
ã¨ã¯è¨ãæææ§æ¬²ããå¢ã¯å°ãªããªãã®ã§ãlmerTest
ããã±ã¼ã¸ã使ããã¨ã§på¤ã欲ãã人ãã¡ãææ¸ã§ãã¾ãã
é層ãã¤ãºã¢ãã«ã«ã¤ãã¦ã¯ã¾ãå¥ã§è¨äºãæ¸ãã¾ããå¤åã
å®è£
ãã¼ã¿
penguins
ãã¼ã¿ãæ¡ç¨ãã¾ãã
iris
ã®ä»£ããã¨ãªããã¼ã¿ã»ããã«ãªãã®ã§ãã¿ããªããã³ã®ã³ãåæãããã
Rã§ã¯palmerpenguins
ããã±ã¼ã¸ã§ã¤ã³ã¹ãã¼ã«å¯è½ã§ãã
library(tidyverse) library(palmerpenguins) summary(penguins)
## species island bill_length_mm bill_depth_mm
## Adelie :152 Biscoe :168 Min. :32.10 Min. :13.10
## Chinstrap: 68 Dream :124 1st Qu.:39.23 1st Qu.:15.60
## Gentoo :124 Torgersen: 52 Median :44.45 Median :17.30
## Mean :43.92 Mean :17.15
## 3rd Qu.:48.50 3rd Qu.:18.70
## Max. :59.60 Max. :21.50
## NA's :2 NA's :2
## flipper_length_mm body_mass_g sex year
## Min. :172.0 Min. :2700 female:165 Min. :2007
## 1st Qu.:190.0 1st Qu.:3550 male :168 1st Qu.:2007
## Median :197.0 Median :4050 NA's : 11 Median :2008
## Mean :200.9 Mean :4202 Mean :2008
## 3rd Qu.:213.0 3rd Qu.:4750 3rd Qu.:2009
## Max. :231.0 Max. :6300 Max. :2009
## NA's :2 NA's :2
ãã¼ã¿ã®ä¸ã«ã¯æ°å¤åã¨å ååããã£ããå ¥ã£ã¦ãããå¤å¤é解æã«åºã使ãããã¼ã¿ã»ããã«ãªã£ã¦ãã¾ãã
ã¢ãã«
ä»åã¯åå帰ã¢ãã«ãèãã¾ãã
å
·ä½çã«ã¯ããã¡ã°ãã®é·ã(bill_length_mm
)ããã翼ã®é·ã(flipper_length_mm
)ãã§å帰ãã¾ãã
ãã®éãã種é¡(species
)ã«ãã£ã¦ãã¡ã°ãã®é·ãã®å¹³åãéãããããï¼ãã¨ãããã¨ãèãã¾ãã
penguins
ãã¼ã¿ã§ã¯ãã¢ããªã¼ãã³ã®ã³(Adelie
)ãã¸ã§ã³ãã¼ãã³ã®ã³(Gentoo
)ããã²ãã³ã®ã³(Chinstrap
)ã¨ãã3種é¡ã®ãã³ã®ã³ã®æ
å ±ãå
¥ã£ã¦ããã®ã§ã
ããå¥ã«ãªããéããããããªããã¨ãã仮説ãèãã¾ãã
g <- ggplot2::ggplot(usedata, aes(x = flipper_length_mm, y = bill_length_mm))+ # scatter plot geom_point(aes(group = species, color = species)) plot(g)
æ£å¸å³ãè¦ãéãããã®ä»®èª¬ã¯ã¾ãã¾ã妥å½æ§ãããããã§ãã
lme4
ãã¨ãlmerTest
ã®å®è£
lme4
ç³»ã®ã¢ãã«ã§ã¯ãé常Rã§ä½¿ãformula
å½¢å¼ãã¤ã¾ãy ~ x + z
çãªè¨è¿°ã®å¿ç¨ã§å¤éå¹æã表ç¾ã§ãã¾ãã
ä»åã¯ã種é¡ã«ãã£ã¦ãã¡ã°ãã®é·ãã®å¹³åã¨ã翼ã®é·ãã§èª¬æã§ãã度åããç°ãªããã¨ãã仮説ãæ¤è¨¼ããã¹ãã 以ä¸ã®ããã«è¨è¿°ãã¾ããã
glmm_model <- lmerTest::lmer( bill_length_mm ~ flipper_length_mm + (1 + flipper_length_mm|species), data = usedata )
çµæãè¦ã¾ãã
summary(glmm_model)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: bill_length_mm ~ flipper_length_mm + (1 + flipper_length_mm |
## species)
## Data: usedata
##
## REML criterion at convergence: 1596.2
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.6469 -0.6595 0.0297 0.6924 4.9957
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## species (Intercept) 2.6567656 1.62996
## flipper_length_mm 0.0009521 0.03086 -1.00
## Residual 6.7064017 2.58967
## Number of obs: 333, groups: species, 3
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 1.25267 4.34480 21.25728 0.288 0.77590
## flipper_length_mm 0.21789 0.02767 3.06788 7.873 0.00394 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## flppr_lngt_ -0.886
## convergence code: 0
## unable to evaluate scaled gradient
## Model failed to converge: degenerate Hessian with 1 negative eigenvalues
Fixed
effects
ã®é
ãåºå®å¹æã®çµæã«ãªãã¾ããlmerTest
ã®ãããã§ãä¸å¿ã翼ã®é·ããã®å¹æã¯ããªãã¨ã¯ãããªããã¨ããããã§ãã
ããã¦ããªãã¨ã¯ãããªããã¨è¡¨è¨ãã¦ãã¾ãããããã¯å帰ä¿æ°ã®æææ§æ¤å®ã«ããã帰ç¡ä»®èª¬ã
ã翼ã®é·ãã®å帰ä¿æ°ãï¼ã§ããããã¨ã«å¯¾ããæ¤å®ãªã®ã§ããããããã®ä¸»å¼µããã§ããªããã¨ã«èµ·å ãã¾ãã
ãå帰ä¿æ°ã«æå³ããããã¨ããã¨ããã«ã¤ãã¦ã¯ãå¤å
¸çãªã¨ããããè°è«ãããã¾ãã
Random effects
ã®é
ãã種é¡å¥ã«ã©ãã ãã°ãã¤ããããããã®å¤éå¹æ(ã®åæ£)ã«ãªãã¾ãã
ãããè¦ãã¨ãåç(âå¹³åçãªãã¡ã°ãã®é·ãã®éã)ã¯ç¨®é¡å¥ã«ç°ãªãã¨è¨ãããã§ããã
翼ã®é·ãã§èª¬æã§ãã度åãã«ã¯ç¨®é¡å¥ã«ã¯å¤§å·®ããªãããã§ãã
ã¡ãªã¿ã«ãå¤éå¹æã®ãã©ã¡ã¼ã¿æ¨å®å¤ã¯ranef()
ã§åºåã§ãã¾ãã
ãã ããã®ranef()
ã§åºåããããã©ã¡ã¼ã¿æ¨å®å¤ã®åæ£ã¯ãsummary
ã§åºåãããåæ£ã¨ç°ãªããã¨ãåãã£ã¦ãã¾ãã
ä¿®æ£ã®æ¹æ³ã¯arm
ããã±ã¼ã¸ã®se.ranef()
é¢æ°ã§åå¤éå¹æã®æ¨æºèª¤å·®ãè¨ç®ãã¦ã
ãã®èª¤å·®ãåæ£ã«å ãããã¨ã§å¯è½ã§ãã詳細ã¯ãã¡ãã®ããã°ãåç
§ãã ããã
å¯è¦å
çµæã®å¯è¦åããã¦ã¿ã¾ãã
æ¯è¼ã¨ãã¦ã種é¡å¥ã«ç¬ç«ã§å帰åæãå®æ½ããçµæãåããã¦è¨è¿°ãã¾ãã
usedata$y_pred_glmm <- fitted(glmm_model) g <- ggplot2::ggplot(usedata, aes(x = flipper_length_mm, y = bill_length_mm))+ # scatter plot geom_point(aes(group = species, color = species)) + # single linear regression model for each species. geom_smooth(aes(group = species, color = species), method = "lm", se = F, linetype= "dashed") + # fitted value from GLMM model. geom_smooth(aes(y = y_pred_glmm, color = species))
ggplot2
ã§ã¯geom_smooth()
ã§æå®ãããã¨ã§ç¨®é¡å¥ã«å帰ç´ç·ãå¼ããã¨ãã§ãã¾ãã
method
ã«"gam"
ãæå®ããã¨ä¸è¬åå æ³çã¢ãã«ã«ãããã³ãã©ã¡ããªãã¯ãªæ²ç·å帰ãã§ãã¾ã*1ã
çµæã¯ãããç ´ç·ã種é¡å¥ã®å帰ãå®ç·ãä¸è¬åç·å½¢æ··åã¢ãã«ã§ã®çµæã§ãã
plot(g)
ç¹ã«ã¸ã§ã³ãã¼ãã³ã®ã³(éããããã)ã§é¡èã§ãããç ´ç·é¨åãè¦ãã¨å³ä¸ã®å¤ãå¤é¨åã«
å帰ç´ç·ããå¼ã£å¼µããã¦ãããããã«è¦ãããã¨ãåããã¾ãã
ä¸æ¹å®ç·é¨åã§ã¯ããã®å¤ãå¤ã«å¼ã£å¼µãããã«ãå帰ä¿æ°ã§è¡¨ç¾ããããå¾ãããç·©ããªã£ã¦ãããã¨ãåããã¾ãã
ããã¦ãã®å¾ã度åãã¯ã種é¡å¥ã«å¤§ããå¤ãããªããã¨ã¯ãè¦è¦çã«ãããããã¨æãã¾ãã
ãããã«
githubè¡ãã°ãããã¨ããstan
ã§é層ãã¤ãºã¢ãã«æ§ç¯æ¸ã¿ã ã£ãããã¾ãã
ãããæ°å¤çã«ãªããå¤ãªã®ã§ *2 ã
ã©ããå¤ãªã®ãæ¢ããªããåæ稿ãã¾ããããã¼ããã