ãã®è¨äºã¯Rè¨èª Advent Calendar15æ¥ç®ã®è¨äºã§ãã
ç§ã¯åè«ãä¿®è«ãçä¸æ¡ä»¶ã¯å
¨é¨Rã§åãæãã¦ãã¦ãç¨åº¦ã«ã¯Rã触ãã¦ãã¦ããããã10å¹´ã¯Rã¨æ®ããã¦ãã¾ãã
ãããã¦ããã¨å½ç¶ãæ§ã
ãªããã±ã¼ã¸ã«åºä¼ããã®ã§ãã¦ãRã¦ã¼ã¶ã¯å¥½ããªããã±ã¼ã¸ãè¿°ã¹ããã¨ãã¢ã¤ãµã代ããã«ãªãã®ã§*1ãä»å¹´ã®ã¢ãã«ã¬ã¯ãããã話ããããããªã¨æãã¾ãã
æ¨ä»ã¯tidyverse
ãtidymodels
ã¨ãã£ãããã±ã¼ã¸ã群ãã注ç®ããã¦ãã¾ãã
ãããã¯Rã®è¯ããæªããã«ãªã¹ãªãã¼ã¿åæã®ãã©ã¼ãããã«ä¸è²«ããææ³ãæã£ã¦ã¡ã³ããã³ã¹ããã¦ãã¾ãã
éã«è¨ãã¨ãããã以å¤ã®ããã±ã¼ã¸ã¯ãåé åã®åé¡ã»é¢å¿ã«å¿ãã¦ç°ãªãã¾ãã
ä½ããRè¨èªã¯ãçµ±è¨è§£æã«ç¹åããè¨èªãã§ããã¯ããªã®ã§ãããã¯ããã¯å¤æ§ãªè§£æææ³ãå®è£
ããã¦ãã¾ãããã ããããæ®å¿µãªãã¨ã«ãçµ±è¨è§£æãã®ããã±ã¼ã¸ã«é¢ãã¦ã¯ããã¾ã§æ³¨ç®ãããªãã®ãã深層å¦ç¿ã大è¦æ¨¡è¨èªã¢ãã«ã注ç®ãããã¢ã«ã´ãªãºã ã«ãªã£ã¦ããç¾ä»£ã®ãã¼ã¿åæå¸å ´ã«ããããå°ããã¿ããã¨ããã§ã
*2ã
ä»äºãä¿®ç¾ å ´ã ããå¿ããã¦ãªã«æ¸ããããªãã¨ãããããªãããã©ããnçªç ãã§ã漸è¿çã«ã¯å 容ã®ãªãè¨äºã«ãªãã®ã§ããããã£ãããªã®ã§ä¸è©±ã«ãªã£ãããã·ã³ãã«ã«å¥½ããªçµ±è¨è§£æããã±ã¼ã¸ã2ã¤ç´¹ä»ãããããªã¨æãã¾ããã æ°ççãªè©±ã¯çµ±è¨ã»æ©æ¢°å¦ç¿ã®æ°çAdvent Calendarã§æ¸ãã¦ã»ããããä½ããæ¸ãã¾ãã
å ååæããã±ã¼ã¸{psych}
ç§ãé·ããã¨èª¿æ»ãã¼ã¿ã®åæã«æã¾ã£ã¦ãã人éãªã®ã§ãã¾ãã¯èª¿æ»ãã¼ã¿ã§ããç¨ããããã±ã¼ã¸ã®è©±ããã¾ãã
ãããããããã¾ããããã¯å人ã®è¦è§£ã§ãã£ã¦ãæå±å
ã代表ããæè¦ã§ã¯ããã¾ããã
ã¾ãããããæ£è§£ã ã¨ãè¨ããã¨ãããã¾ããã好ããªããã±ã¼ã¸ãè¨ãããã{psych}
ã
{psych}
ã¯å¿ç測å®çè«ãå®é¨å¿çå¦ã«ç¨ããåæå
¨è¬ãå¹
åºãã«ãã¼ããããã±ã¼ã¸ã§ãã大å¦ã§ãä»äºã§ãã調æ»ãã¼ã¿ã使ã£ãåæã§ä¸è©±ã«ãªããªããã¨ããªããããããã使ã£ã¦ãã¾ã(ãã¾ãã)ã
調æ»ãã¼ã¿ã®åæ
調æ»è¦³å¯ãã¼ã¿ã¯ãå½å¢èª¿æ»ãå§ãã¨ãããç·åççµ±è¨å±ãè¡ãå種ä¸å¸¯èª¿æ»ã¯ãã¡ããã ç£æ¥çã«ããã¦ããã©ã³ãã®ã¤ã¡ã¼ã¸ãä¸ä»£ã®ä¾¡å¤è¦³ã®å¤åãã¦ã¼ã¶è¦³ç¹ããã®ãããã¯ãè©ä¾¡ãªã©ã ãã¼ã¿ã®éã®è¨å¤§åã»è³ªã®å¤æ§åãé²ãã ç¾ä»£ã«ããã¦ã測å®ã®é£ããã人ã®æèãç¶æ ã ã測å®ããããã«éè¦ãªä½ç½®ãå ãã¦ãã¾ããããã¼ã±ãã£ã³ã°ãªãµã¼ããã¨ããæ¥çã»æ¥ç¨®ãããããã«ã ããã測å®ããããã®æè¡ã»ãã¦ãã¦ãç£æ¥ç価å¤ããã£ã¦ãããã¨ã¯ä¸å®äºå®ã ã¨æãã¾ãã
ããã§ã¯ãæ¶è²»è ããã®ãè²·ãã¨ãã«èãã¦ãããã¨ããç¥ããã¨ãä¾ã«åãã¾ã
調æ»ãã¼ã¿ã¯ãå®æ½ããå´ãç¥ããããã¨ã測ããããã¨ã測ããã¨ãã§ããã¨ããå¼·ã¿ãããåé¢ã ãã¨ãã°ãããªããååãè²·ãã¨ãã«èãããã¨ã¯ãªãã§ããï¼ãã¨èãã¦ããæ確ãªçããåçè ããå¾ããã¨ã¯ é£ããã¨æãã¾ãããã®ãããååãè²·ãã¨ãã¯ä¾¡æ ¼ãéè¦ãã¦ãããã¨ããæåãªã¡ã¼ã«ã¼ãåºãã¦ãããã®ãè²·ãã ãç°å¢ã«åªããååãè²·ãããªã©ãããã¤ãé¸æè¢ãæ示ããªãããåçè ãååãè²·ãã¨ãã«ã©ããªãã¨ãæèãã¦ããããã å¯è½ãªéãå¼ãåºããã¨è©¦ã¿ã¾ãã
ãããªä¸ã§èµ·ããæé»ã®ããåãã«ãåçè ã«å¯è½ãªéãããããã®ãã¨ãèããããã¨ããå®æ½å´ã®ãã¼ãºã¨ã ãé·ãã¢ã³ã±ã¼ãã¯ã ãããã¨ããåçè å´ã®äºæ (åçè² è·)ã§ãããããªãã¨ããã¾ãæè¡ãããã¨ã ã¤ã¾ããå¯è½ãªéãçãã¢ã³ã±ã¼ãã§ãå¯è½ãªéãå¤ãã®æ å ±ãå¼ãåºãããã¨ãã¢ã³ã±ã¼ããã¼ã¿ã§ã¯æ±ãããã¾ãã
次å éç´ææ³ã¨ãã¦ã®å ååæã»ä¸»æååæ
ããããæèã測å®ããã¨ãã«ããç¨ããããã¢ããã¼ãã¯ãå®æ½ããå´ã§èãããéãã®
ããã®ãè²·ãã¨ãã«èãã¦ãããã¨ãã®ä»®èª¬ãåºãåã£ã¦ãä½ããã®å½¢ã§éç´ããããã¨ã§ãã
ãã®ã仮説ãåºãåããã¨ããã®ã¯ã¾ãã¾ãã®é£æ度ãé«ãã®ã§ãããä»åã¯çµ±è¨è§£æããã±ã¼ã¸ãç´¹ä»ããè¨äºãªã®ã§ã
ãã®è¾ºç¥ããã人ã¯bob3ããã¨ãã«èãã¦ä¸ãã調æ»ä¼ç¤¾ã¨ãã«ä¾é ¼ãã¦ãã ããã
ãã¦ãã©ãã«ã仮説ãåºãåã£ã¦ãåçãã¦ããã£ããã¨ãä»®å®ãã¦è©±ãé²ããã¨ã å¾ãããåçãã©ã®ããã«éç´ããããèããå¿ è¦ãããã¾ãã ãã¡ãããäºåã«ãä¾¡æ ¼ç³»ã®è³ªåããç°å¢ç³»ã®è³ªåãã®ããã«ãã£ã¡ãè¨è¨ãã¦ããããã¾ã¨ãã¦ãè¯ãã®ã§ããã ãã®æããã©ã®è¨åãã©ã®ç¨åº¦ã®éã¿ã§ã¾ã¨ãããããã®ããã解決ãããã¨ã¯é£ããããããã¾ããã
ããããåé¡ã«å¯¾ãã¦ãããç¥ããã解æææ³ã¯ãå ååæããã主æååæãã¨è¨ããããã®ãç¥ããã¦ãã¾ãã å ååæã¯ç¹ã«å¿çå¦ã®é åã§è¦ããã¾ããã主æååæã社ä¼ç§å¦åéã§ç¨ãããã¦ãã¾ãããã ä»ã¯æ©æ¢°å¦ç¿ã§ã®æ¬¡å 縮ç´ã¢ã«ã´ãªãºã ã¨ãã¦ã®ä½ç½®ã¥ãã大ããããããã¾ããã
Rã§å ååæãããå ´åã{psych}
ããã±ã¼ã¸ã§ã®å®è£
ãç¥ããã¦ãã¾ãããå¤åãããä¸çªã·ã³ãã«ã«å®è¡ã§ããã¨æãã¾ãã
ä½ãªã主æååæã¯ããã©ã«ãã®ããã±ã¼ã¸{stats}
ã«prcomp()
ã¨ãã¦å®è£
ããã¦ãã¾ãã
ä»åã¯çµ±è¨è§£æããã±ã¼ã¸ãç´¹ä»ããè¨äºãªã®ã§ã{psych}
ããã±ã¼ã¸ã§å ååæããã£ã¦ã¿ããã¨æãã¾ãã
å ååæã主æååæãå
±éãã¦ãå å(主æå)ã®æ°ãã©ã®ç¨åº¦ã«ãããï¼ãã¯äººéãä½ããã®åºæºã§æ±ºãããã¨ã«ãªãã¾ããå ååæã®å ´åãè¯ãç¨ããããææ³ã¯ã¹ã¯ãªã¼ããããã§ããã{psych}
ã§ããã°ãããå®æ½ã§ãã¾ãã
ã¹ã¯ãªã¼ããããã¯ç¬¬ä¸å åã®ç¹ç°å¤ã縦軸ãå åæ°ã横軸ã«ã¨ã£ãæãç·ã°ã©ãã§ã{psych}
ã§ã¯ããã«å ãã¦å¤å¤éæ£è¦åå¸ã«å¾ãåæ£å
±åæ£è¡åã®ç¹ç°å¤å解ãããã¨ãã®ã·ãã¥ã¬ã¼ã·ã§ã³ãã¼ã¿ã¨æ¯è¼ããäºãã§ãã¾ãã
library(tidylog) library(psych) # psychã«ãããµã³ãã«ãã¼ã¿ã使ã£ã¦ã¿ã data(bfi) bfi_to_pca <- bfi |> tidylog::select(-gender, -education, -age) # ã¹ã¯ãªã¼ãããã psych::fa.parallel( bfi_to_pca ) R> Parallel analysis suggests that the number of factors = 6 and the number of components = NA
ã¹ã¯ãªã¼ããããã¯ãã¡ã
ãããè¦ã¦ãé©ããå åæ°ã¯ã·ãã¥ã¬ã¼ã·ã§ã³ã§å¾ããããã¼ã¿(赤ç·)ãä¸åãããããé©ãã¦ãã6ï½7ããããã¡ããã©ããã¨å¤æãã¾ãããªããªãRããå åæ°6ãæ¨å¥¨ãã¦ãã¾ã*3ã
å åæ°ã6ã¨è¨å®ããã¨ãã«å ååæãå®æ½ãã¦ã¿ã¾ãããã
fa_result <- psych::fa( bfi_to_pca, nfactors = 6, rotate = "promax", fm = "ml") summary(fa_result) R> Factor analysis with Call: psych::fa(r = bfi_to_pca, nfactors = 6, rotate = "promax", fm = "ml") R> R> Test of the hypothesis that 6 factors are sufficient. R> The degrees of freedom for the model is 165 and the objective function was 0.36 R> The number of observations was 2800 with Chi Square = 1013.79 with prob < 4.6e-122 R> R> The root mean square of the residuals (RMSA) is 0.02 R> The df corrected root mean square of the residuals is 0.03 R> R> Tucker Lewis Index of factoring reliability = 0.922 R> RMSEA index = 0.043 and the 10 % confidence intervals are 0.04 0.045 R> BIC = -295.88 R> With factor correlations of R> ML1 ML2 ML3 ML5 ML4 ML6 R> ML1 1.00 -0.36 0.41 0.33 -0.11 0.09 R> ML2 -0.36 1.00 -0.21 -0.18 0.11 0.26 R> ML3 0.41 -0.21 1.00 0.31 -0.15 0.20 R> ML5 0.33 -0.18 0.31 1.00 0.03 0.28 R> ML4 -0.11 0.11 -0.15 0.03 1.00 0.09 R> ML6 0.09 0.26 0.20 0.28 0.09 1.00 fa_result$weights # å åè² è·é R> åå ¥åå¤æ°ããåå åã«ãããå åè² è·éãã§ã¦ãã fa_result$scores # å åç¹ç¹è¡å R> ãµã³ãã«å¥ã«ãåå åã«å¯¾ããç¹å ¸ãå¾ããã
ãããªæãã§ãåºæ¬çãªåºåã¯ããç¨åº¦ãã£ããåºãã¦ããã¾ããçã«çã¨ãããã®ããããªãã°ã
å åã®ã©ãã«é åºããå¿
ãããæé ã§ã¯ãªããã¨ããã¨ããã§ããããããã°ãªã®ãä½ãªã®ãâ¦â¦ã
ã¡ãªã¿ã«ä¸»æååæã¨å ååæã¯ã©ã¡ããç¹ç°å¤å解ãå ã¨ããçµ±è¨è§£æææ³ã§ãããä»åã¯çµ±è¨è§£æããã±ã¼ã¸ãç´¹ä»ããè¨äºãªã®ã§ããããã®è©±ã¯çµ±è¨ã»æ©æ¢°å¦ç¿ã®æ°çAdvent Calendarã«è¨è¼ã§ãããããã§ããã
å帰åæ
æ©æ¢°å¦ç¿ã¨çµ±è¨å¦ã®å ±éé¨åã«ãããçµ±è¨è§£æã£ã½ãåé¡ã«ãå帰åé¡ããããã¨æã£ã¦ãã¾ãã ç§ã¯å帰åæã大好ãã§ãå帰åæãé£ã¹ã¦çãã¦ããã¨è¨ã£ã¦ãéè¨ã§ã¯ãªãã§ãããã
Rã§ã¯ããã©ã«ãã®ããã±ã¼ã¸{stats}
ã§ããããæ®éã®å帰åæãlm()
ããã¸ã¹ãã£ãã¯å帰åæãªã©
ãããããä¸è¬åç·å½¢ã¢ãã«ããglm()
ã§å®è¡ã§ãã¾ãã
ä½ã追å ããã±ã¼ã¸ãå
¥ããªãã¦ãå帰åæãã§ããããã°ã©ãã³ã°è¨èªã¯ãããªã«ãªãã¨æãã®ã§ãããã¯ãããã¨æãã¾ãã
ç§ã好ããªã®ã¯æ´ã«è¤éãªã¢ãã«ã§ãä¸è¬åç·å½¢æ··åã¢ãã«ã¨å¼ã°ãããã®ãããã¾ãã ãã§ã«éå»ã«åæ§ã®è¨äºãæ¸ãã¦ãããã§ãããä½åº¦æ¸ãã¦ãè¯ãã®ã§æ¸ãã¾ãã
ä¸è¬åç·å½¢æ··åã¢ãã«ã¯ãã«ãã¬ãã«ã¢ãã«ã¨ãã©ã³ãã å¹æã¢ãã«ã¨ã ãããããªé åã§å¥ã®ååãã¤ãã¦ãã¦é¢åã§ããããã®è¨äºã§ã¯ ãç¹å®ã®ãµãã°ã«ã¼ãã«ãã£ã¦ãã©ã¡ã¼ã¿ã®å¹æãå¤ããããã¨ãã«æ¡ç¨ããã¢ãã«ãã¨ãã¦ãªãã¨ãåãæãã¾ãã
éå»ã«æ¸ããè¨äºã§ã{lme4}
ãç´¹ä»ãã¦æ¥ã¾ãããä½ãªãä¿®è«ã®ä¸é¨ã«ã使ã£ã¦ãã¦ãã¾ãããã®ããã±ã¼ã¸ã¯glm
ã©ã¤ã¯ã«ä¸è¬åç·å½¢æ··åã¢ãã«ãè¨è¿°ãããã¨ãã§ãã¾ãããåºå®å¹æã®å帰ä¿æ°ã®æææ§æ¤å®ãåºãã¾ãããããã¯è²ã
ã®è°è«ãããã¾ããä»ã®ç§ã®ç解ã§ã¯ã帰ç¡ä»®èª¬ã®ã¨ãã«å¾ãåå¸ãã®ä»®å®ã¯ãæ¬å½ã«å®æ
ã¨ãã¦æºããããã®ãï¼æºããããªãä¾ãä½ãã¦ããããã¨ããã¨ãããããæ¤å®ã«ç¨ããéã¯åºããããã«ããªããããå
·ä½çãªæ¤å®ã¯ããã¦å®è£
ããªããã¨ããã®ã{lme4}
ã®éçºå´ã®ã¹ã¿ã³ã¹ã§ããã¨èãã¦ãã¾ãã
ä»åã¯çµ±è¨è§£æããã±ã¼ã¸ãç´¹ä»ããè¨äºãªã®ã§ãå
·ä½çã«å帰ä¿æ°ã®çµ±è¨çæ¤å®ãã©ããªã£ã¦ããã®ãã¯ç«¯æãã¾ãã
ãããã«ãã¦ãã{lme4}
ã¯æ··åã¢ãã«ã大ããªã¢ãã«ã«å¯¾ãã¦é«éãªè¨ç®ã®å
å®ç¾ã§ãããã¨ã§ãè¯ããæªããå°ã£ã¦ããã¨ããã好ãã§ãããªããããã£ãããã
ããã{lme4}
ãã¼ã¹ã§æ··åã¢ãã«ã®å帰ä¿æ°ã®æ¤å®ãå®æ½ãããå ´åã{lmerTest}
ã¨ããããã±ã¼ã¸ãããã¾ã*4ã
å®è£
ã«ã¤ãã¦ã¯ä»¥ä¸ã®éå»è¨äºãè¦ãã»ããã¯ããã§ããæè¿ã¯éå»ã®èªåãè¶ããããäºãã§ããªãã
ããã§åºããçµæããã¼ã¿ãç¹å®ã®ç¾¤å¥ã«æ¸¬å®ããããã¼ã¿(å
¥ãåãã¼ã¿ããã¹ããããã¼ã¿)ãã1ã¤ã®ã¢ãã«ã§ãã¾ããã¨è¡¨ç¾ã§ããã®ãã
å²ã¨ãã£ããããªãã¨æãã¾ãããï¼ç§ã¯ãã£ãããã¨æãã¾ãã
çµããã«
ç§ã好ããªRããã±ã¼ã¸ã¯{psych}
ã¨{lme4}
ã§ããçããã®å¥½ããªããã±ã¼ã¸ã¯ãªãã§ããï¼
åèæç®
*1:ãããªãã¨ããªã
*2:ãããã®çºå±ã¯äººé¡ã«å¤§ããå¯ä¸ãããã®ã§ãã確信ã¯ããã¾ããã測å®ã§ãããã¼ã¿ã®ç¨®é¡ã¨éãå¢ãããã¨ã§ã æ°ççµ±è¨å¦ãçºå±ããæ代ã®å¶ç´ã®ä¸é¨ãåãæããããã¨ã¯ééãã¯ãªãã¨æãã¾ãã ããããèªããããã§ãç§ã¯ãã®è¶¨å¢ããå人çã«ãæ°ã«é£ããªãã¨ããã ãã§ãã
*3:å®åä¸ã¯ããåºãã«ã5ï½9ãããã®å åæ°ã§ä¸æ°ã«åãã¦ãé©åãªè§£éãå¯è½ãªè¦ç´ ãåºããããªãã§ãªã¼ãããã³ã°ãã¾ã¾è¡ãã¾ãã
*4:ãã ããå®æ½å¯è½ãªã®ã¯lmerã®ã¿ãä¸è¬åç·å½¢æ··åã¢ãã«glmerã«ãããæ¤å®ã¯ããã§ããã£ã¦ãããªãããã©ã³ãã å¹æã«å¯¾ããæ¤å®ã¯éå¢ã§ããã