RStudioããknitrã§ã¬ãã¼ããèªåä½æãã¦ã¿ã
ããã«ã¡ã¯ããªã½ããµ*1ã§ããªãã¿ã®æã§ãããçãªã®ã§BON JOVIèãã¦ãã¾ã*2ãè«æã¯ã¾ã æ¸ãã¦ãã¾ãããã©ä½ãã
ãã¦ãããããTokyo.Rã®åç°è¨ä¹ããã¨ããæ¹ã®ã¡ããç´ æ´ããããã¬ã¼ã³ãã¡ã¤ã«ãæè¦ãããã¾ããã
そろそろRStudioの話でもしてみようと思う
ããã§RStudioããã¬ãã¼ããèªåä½æã§ããknitrã¨ãããã¼ã«ãç´¹ä»ããã¦ããã®ã§ãèªåã§ãå°ãããã£ã¦ã¿ã¾ãããä»åã¯ãã®èªç¿ã®ã¾ã¨ããå
¼ãã¦ããã«ãæ¸ãã¦ã¿ããã¨æãã¾ãã
ããããï¼Rã®çµæãã©ããã£ã¦ä¿åãã¦ã¾ãï¼
ããã¨ãã¾ãããããã®åé¡æèãªã®ã§ãããã¿ãªãã¾ã¯Rã§ã®è¨ç®çµæã£ã¦ã©ãä¿åãã¦ã¾ããï¼
ããã£ã¦çµæ§ã¡ã³ãã¯ãµã¤åé¡ã§ããã¾ã¾ã§ç§ã¯åºæ¬çã«ï¼
- ããã¹ãç³»ã®ã¢ã¦ããããã¯ããã¹ããã¡ã¤ã«ã¸æ¸ãåºãï¼ã¾ãã¯ã³ã³ã½ã¼ã«ããã³ããï¼
- ç»åç³»ã®ã¢ã¦ããããã¯png/PDFãªã©ã¸æ¸ãåºã
ãã¦ãã¾ããã
ãã®ã¨ãã«ããªãããã¾ãæ¹æ³ãªãããªããã¨å¸¸ã æã£ã¦ããã®ãããæ¸ãã ããçµæã®ãã¡ã¤ã«ãã¨ããã®çµæãç®åºããéã«ç¨ãããã©ã¡ã¼ã¿ãã½ã¼ã¹ã³ã¼ããã®å¯¾å¿ä»ãã®åé¡ã§ãã
Rã§è¡ã£ãè¨ç®ã®çµæããã©ã®ãããªãã©ã¡ã¼ã¿ãã½ã¼ã¹ã³ã¼ãã®ãã¨ã§çæããããã®ãªã®ããã¡ããã¨è¨é²ãã¦ããã®ã¯ã¨ã£ã¦ã大äºã§ãããããããã½ã¼ã¹ã³ã¼ãããã©ã¡ã¼ã¿ããã³ã³ã½ã¼ã«ã¸ã®ã¢ã¦ããããããã°ã©ãç»åãã¡ã¤ã«ããªã©ã®ç°ãªãå½¢å¼ã®ãã®ãã¹ãã¼ããã¤åãããããå½¢ã§ã¾ã¨ãã¦ããã®ã¯æ¡å¤ãããããã§ãããã
ã§ãããï¼
ããã§ãknitrãªã®ã§ããï¼ï¼ãã¤é¡ï¼
knitrã£ã¦ãªãããï¼
knitrã¨ã¯ãã®開発者サイトã«ããã¨ã"Elegant, flexible and fast dynamic report generation with R"ã¨ãããã®ãããã§ãã
èªåã§"elegant"ã¨ãè¨ããããã¨ä¸ç¬æãããããã¾ããã*3ãè¦ããã«Rã®ã½ã¼ã¹ã³ã¼ããããã½ã¼ã¹ã³ã¼ãããã©ã¡ã¼ã¿ããã³ã³ã½ã¼ã«ã¸ã®ã¢ã¦ããããããã°ã©ãç»åãã¡ã¤ã«ããªã©ã®ç°ãªã£ãå½¢å¼ã®ãªãã¸ã§ã¯ããæ··å¨ããã¬ãã¼ããèªåçã«çæã§ãããã¼ã«ã§ãã
ã¨ããããææ°ç(0.96.330)ã®RStudio ã«ã¯å®è£ ããã¦ããã®ã§ä½¿ã£ã¦ã¿ã¾ããããç¾èã¯ä¸è¦ã«ã¹ã¬ã·ã«ãªã§ãã
ã¨ããããã¯ãknitrããã±ã¼ã¸ãã¤ã³ã¹ã³ãã¦ããã¾ããã*4ã
install.packages("knitr")
ã§ãRStudioããknitrã¸ã®æã¯æå¤ã¨è¿ãã«é ããã¦ãã¾ããæ°è¦ãã¡ã¤ã«ã¨ãã¦"R Markdown"å½¢å¼ã®ãã®ãéãã¦ã¿ã¾ãããï¼
ããããã¨æ°è¦ãã¡ã¤ã«ã¨ãã¦"untitled.Rmd"ã¨ããåã®ããã©ã«ãã®ãã¡ã¤ã«ãRStudioå ã®å·¦ä¸ã¦ã£ã³ãã¦ã§éãã¾ãããããããé©å½ãªååï¼test.Rmdã¨ãï¼ãã¤ãã¦ãã®ãã¡ã¤ã«ãä¿åãã¦ããã"Knit HTML"ã¨æ¸ãã¦ããã¢ã¤ã³ã³ãã¨ããããã¯ãªãã¯ãã¦ã¿ã¾ãããã
ããããã¨ãMarkdownå½¢å¼ã§æ¸ãããå·¦ä¸ã¦ã£ã³ãã¦å ã®Rmdãã¡ã¤ã«ãhtmlå½¢å¼ã«å¤æããã¦è¡¨ç¤ºããã¾ããã¨ãããããã¬ãã¥ã¼ç»é¢ãåºãã®ã§ãå¿ è¦ã«å¿ãã¦"Save As"ãã好ã¿ã®å½¢å¼ã§ä¿åãããã§ãã¾ã*5ã
ã¾ãåºæ¬çãªæ®µåãã¯ã ããããããªãããã§ãï¼âéï¼ã
ããã¨ããã®Markdownã£ã¦ãªãããï¼
Markdownã¨ããã®ã¯ãè¦ããã«ãã¯ã¦ãªè¨æ³ãã¿ãããªããã§ããããä¸è¬çã«è¨ãã°ãhtmlã¨ãLaTexã¨ãã®ç°¡æçã¿ãããªãã®ãã¤ã¡ã¼ã¸ããã°ããããããã¾ããã
ã¨ããããRçMarkdownã®ææ³ãªã©ã®æ
å ±ã¯ãã¡ãï¼
Using Markdown with RStudio
Markdownã®ææ³ã®è©³ããæ
å ±ã¯ãã¡ãï¼
blog::2310 » Markdown文法の全訳
ãã¨ãRstudioã§ã®"knit HTML"ã®å·¦å´ã«ãã"MD"ã¨æ¸ãã¦ããã¢ã¤ã³ã³ãæ¼ãã¨Markdownã®ææ³ã®ãã«ããåºã¦ãã¾ãã
ä¸è¨ã®ãããªMarkdownææ³ã®ã³ã¼ãã交ãã¦Rã®ã³ã¼ããæ¸ããããã§knitrã使ãã¨ã¬ãã¼ããèªåçæã§ãããããªã®ã§ãã
ã¨ãããããµã³ãã«è§£æãã¡ã¤ã«ãæ¸ãã¦ã¿ãã®ã§æãã¦ã¿ã
ã¨ã¯ããã¨ã£ã¤ãã«ããã¨æãã®ã§ãèªä½ã®Rmdãµã³ãã«è§£æãã¡ã¤ã«ãæãã¦ã¿ã¾ãã以ä¸ã§ã¯æ³¨ãå ¥ããããã«ç»åã«ãã¦ãã¾ãããå®éã«RStudioå ã§è©¦ãã¦ã¿ããæ¹ã¯ãã¡ãã®blog.Rmd ãã¡ã¤ã«ãRStudioã§éãã¦ã¿ã¦ãã ããã
ã¡ãªã¿ã«ä»¥ä¸ã®ã³ã¼ããRStudioã§éãã"knitr html"ã®ã¢ã¤ã³ã³ããããã¨ãããããããã®htmlãçæããã¾ã*6âサンプルhtml
ã追å ãä¸è¨ã®ç»åã§ã¯è¦ãããããã¨æã£ãã®ã§ä¸å¿ãã¿ããã¹ãçã追å ãã¾ããâï¼ï¼ãªã¹ãã®é¨åã®"*"ãã¯ã¦ãªè¨æ³ã¨å¹²æ¸ããããã®ã§ããã ãæ¹å¤ãã¦ã¾ãï¼
è«ææ°ã¨tweetæ°ã®é¢ä¿ã«ã¤ãã¦ã®ç·å½¢å帰åæ ======================================================== ## èæ¯ è¿å¹´ãæ¥æ¬ã®ç 究è ã®è«æçç£æ°ã®æ¸å°ãææããã¦ããããã®åå ã¨ãã¦ãtwitterã«ããç 究æéã浪費ããã¦ããå¯è½æ§ãææããã¦ãããæ¬è§£æã§ã¯ãè«ææ°ã¨tweetæ°ã®é¢ä¿ã«ã¤ãã¦äºåçãªçµ±è¨è§£æãè¡ã£ãã ## ãã¼ã¿ ãã¼ã¿ã¯Counterfactual News (2011)ã®èª¿æ»å ±åæ¸ããå¼ç¨ããããã㧠*tweet* ã¯ä¸ã¶æãããã®tweetæ°ã *article* ã¯éå»5å¹´éã®è«ææ°ï¼ç¬¬ä¸èè ã®ã¿ï¼ã§ããã ```{r data} tweet <- c(259, 265, 367, 699, 293, 395, 843, 986, 833, 373, 560, 704, 394, 138, 881, 17, 873, 157, 764, 250, 24, 583, 81, 946, 175, 534, 458, 168, 32, 776, 616, 292) article <- c(7.41, 6.35, 6.33, 2.01, 7.07, 6.05, 3.57, 0, 2.67, 7.27, 1.40, 4.96, 7.06, 9.62, 0, 4.83 , 0.27, 12.43, 4.36, 5.50, 8.76, 3.17, 8.19, 0, 7.25, 3.66, 7.42, 6.32, 8.68, 1.24, 0.84, 8.08) ``` ## æ£å¸å³ æ£å¸å³ã¯ä»¥ä¸ã®éãã§ãããæçãªè² ã®å¾åãã¿ãããã ```{r scatter_plot, fig.width=5, fig.height=5} plot(article ~ tweet, xlab="Number of Tweet (per Month)", ylab="Number of Articles (past 5 years)") ``` ## ç·å½¢å帰 ç·å½¢å帰ãè¡ã£ãã¨ããã以ä¸ã®ãããªçµæãå¾ããããtweetæ°ã®å¹æã¯ææã§ããï¼p<0.001ï¼ã1 tweetãããç´0.009æ¬åã®è«ææ°ã®æ¸å°ã«ç¹ãããã¨ã示åãããã ```{r linear_regression, fig.width=5, fig.height=5} lm_res <- lm(article ~ tweet) plot(article ~ tweet, xlab="Number of Tweet (per Month)", ylab="Number of Articles (past 5 years)") abline(lm_res,col=2) summary(lm_res) ``` ## æ®å·®åæ ç·å½¢å帰ã®ãã§ãã¯ã®ãããæ®å·®ããããããã³æ£è¦Q-Qãããããè¡ã£ããè«ææ°ã¯ã«ã¦ã³ããã¼ã¿ã§ãããããæ¬æ¥ãªãã°ãã¯ã½ã³ã¢ãã«ãç¨ããã¹ãã§ããããæ®å·®ããããããã³æ£è¦Q-Qããããã®çµæãè¦ãéããæ®å·®ã¯è¯å¥½ãªæ£è¦æ§ã示ãã¦ããã¨å¤æã§ããã ```{r regression_check, fig.width=5, fig.height=5} plot(lm_res, which=1:2) ``` ## èå¯ ä»åã®è§£æãããtweetæ°ãè«ææ°ã«ææãªå½±é¿ããããããã¨ã示åããããããããªããããã®å æé¢ä¿ã®åãã«ã¤ãã¦ã¯ï¼ tweetæ°ã®å¢å â ç 究ããã¾ããããªã ç 究ããã¾ããããªã â tweetæ°ã®å¢å ã®ï¼ã¤ã®ç°ãªãå¯è½æ§ãåå¨ãããä»å¾ããã®å æé¢ä¿ã®åããæããã«ããããã«ãè¥æç 究è ã対象ã¨ããååãã³ãã¼ãç 究ãå¿ è¦ã§ããã ## åèæç® [æ´¥ç°å¤§ä»ï¼2012ï¼Twitter社ä¼è«~æ°ããªãªã¢ã«ã¿ã¤ã ã»ã¦ã§ãã®æ½®æµ](http://www.amazon.co.jp/Twitter%E7%A4%BE%E4%BC%9A%E8%AB%96-~%E6%96%B0%E3%81%9F%E3%81%AA%E3%83%AA%E3%82%A2%E3%83%AB%E3%82%BF%E3%82%A4%E3%83%A0%E3%83%BB%E3%82%A6%E3%82%A7%E3%83%96%E3%81%AE%E6%BD%AE%E6%B5%81-%E6%96%B0%E6%9B%B8y-%E6%B4%A5%E7%94%B0-%E5%A4%A7%E4%BB%8B/dp/4862484824/ref=sr_1_7?ie=UTF8&qid=1344845029&sr=8-7) #### ãã®ãã¡ã¤ã«ã®ä½ææ¥æ ```{r timelog} Sys.time() ```
使ãéã¯ï¼
ããããRStudio-knitr-Rmdã®çµã¿åããã¯ï¼ç 究è ã«ã¨ã£ã¦ãï¼ä»¥ä¸ã®ãããªä½¿ãéãããã®ã§ã¯ãªãã§ããããï¼
- èªèº«ã®Rã³ã¼ãã®éçºã»è§£æçµæã®ç§çãã°ç®¡ç
- å ±åç 究è ã¨ã®çµ±è¨è§£æã«é¢ããããã¨ãã®ããã®ãã¼ã«ã»ã¤ã³ãã©ã¨ãã¦
- ç 究室ãªã©ã§ã®ã«ã¼ãã£ã³çµ±è¨è§£æã®ãã³ãã¬ã¨ãã¦æ¡ç¨ãã
- è«æã®Supplementary Materialsã«ãã®ã¾ã¾ä½¿ãã®ããããã¨æã
- RãRStudioã®è¬ç¿è³æã®ä½æã«ã使ããã
ä»åã¯ãã¾ã詳ãããã¨ãæ¸ãã¾ããã§ããã*7ããã詳ããTipsã«ã¤ãã¦ã¯knitrの開発者のサイトãRMarkdownのサイトã和田さんのプレゼン資料ãé©å®ãåç
§ããã ããã°ã¨æãã¾ãã
ã§ã¯ãããçãï¼
.
*1:è«æãæ¸ãã®ãé ãä¸ã«ããµã¤ã¯ãã®ç¥
*2:å
*3:ã§ã使ã£ã¦ã¿ãããããã«elegantã ã£ãã
*4:R2.14.1以ä¸ãå¿ è¦ããã
*5:"publish"ãæ¼ãã¨RPubsã¨ããã³ã¼ãå ¬éãµã¤ãã¸ã®æ稿ã¨ãªãã¾ã
*6:ç»åãã¡ã¤ã«ãhtmlå ã«åãè¾¼ã¾ãã¦ããã®ã§htmlãã¡ã¤ã«ä¸ã¤ã§ã¬ãã¼ãã¯å®çµãã¦ããã½ããããã¯ããããããã
*7:ãããã詳ãããã¨ç¥ããªãã®ã§ãç³ã訳ãªãã£ã