今回は2年以上前のエントリの続きである。
一番下の節で紹介しているtexregパッケージを実戦で使ってみたのだが意外に使いにくい。
確かに、HTML形式で綺麗な回帰分析は作れるが、それをRstudioに張り付けてレポートにすること、つまりknitすることができないのである。
今回はその解決法を紹介する。
下準備
以前のエントリ通り回帰分析を作成する。
library(AER) #パッケージの呼び出し data("CPS1985") lm0 <- lm(wage ~ age, data=CPS1985) #年齢が賃金を決めるモデル lm1 <- update(lm0, ~. + gender) # 性別をモデルに加える lm2 <- update(lm1, ~. + education) # 学歴をモデルに加える lm3 <- update(lm2, ~. + experience) # 仕事の経験年数をモデルに加える
texregによる整形
texregによるHTML形式による出力。詳しくは以前のエントリを参照のこと。
library(texreg) output <- htmlreg( list(lm0, lm1, lm2, lm3), caption.above = TRUE, caption = "表1 回帰分析のモデル比較", custom.coef.names = c("定数","年齢", "性別-女性", "教育年数", "就労経験") )
textreg関数で書き出すと以下のようなHTMLのコードが出力される。
今回はoutput
というオブジェクトに格納しているので、下のようなコードは出力されない。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <table cellspacing="0" align="center" style="border: none;"> <caption align="top" style="margin-bottom:0.3em;">表1 回帰分析のモデル比較</caption> <tr> <th style="text-align: left; border-top: 2px solid black; border-bottom: 1px solid black; padding-right: 12px;"><b></b></th> <th style="text-align: left; border-top: 2px solid black; border-bottom: 1px solid black; padding-right: 12px;"><b>Model 1</b></th> <th style="text-align: left; border-top: 2px solid black; border-bottom: 1px solid black; padding-right: 12px;"><b>Model 2</b></th> <th style="text-align: left; border-top: 2px solid black; border-bottom: 1px solid black; padding-right: 12px;"><b>Model 3</b></th> <th style="text-align: left; border-top: 2px solid black; border-bottom: 1px solid black; padding-right: 12px;"><b>Model 4</b></th> </tr> <tr> <td style="padding-right: 12px; border: none;">定数</td> <td style="padding-right: 12px; border: none;">6.17<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">6.93<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">-4.84<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">-1.96</td> </tr> <tr> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;">(0.72)</td> <td style="padding-right: 12px; border: none;">(0.72)</td> <td style="padding-right: 12px; border: none;">(1.24)</td> <td style="padding-right: 12px; border: none;">(6.84)</td> </tr> <tr> <td style="padding-right: 12px; border: none;">年齢</td> <td style="padding-right: 12px; border: none;">0.08<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">0.09<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">0.11<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">-0.37</td> </tr> <tr> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;">(0.02)</td> <td style="padding-right: 12px; border: none;">(0.02)</td> <td style="padding-right: 12px; border: none;">(0.02)</td> <td style="padding-right: 12px; border: none;">(1.12)</td> </tr> <tr> <td style="padding-right: 12px; border: none;">性別-女性</td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;">-2.27<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">-2.34<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">-2.34<sup style="vertical-align: 0px;">***</sup></td> </tr> <tr> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;">(0.43)</td> <td style="padding-right: 12px; border: none;">(0.39)</td> <td style="padding-right: 12px; border: none;">(0.39)</td> </tr> <tr> <td style="padding-right: 12px; border: none;">教育年数</td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;">0.83<sup style="vertical-align: 0px;">***</sup></td> <td style="padding-right: 12px; border: none;">1.31</td> </tr> <tr> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;">(0.07)</td> <td style="padding-right: 12px; border: none;">(1.12)</td> </tr> <tr> <td style="padding-right: 12px; border: none;">就労経験</td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;">0.48</td> </tr> <tr> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;"></td> <td style="padding-right: 12px; border: none;">(1.12)</td> </tr> <tr> <td style="border-top: 1px solid black;">R<sup style="vertical-align: 0px;">2</sup></td> <td style="border-top: 1px solid black;">0.03</td> <td style="border-top: 1px solid black;">0.08</td> <td style="border-top: 1px solid black;">0.25</td> <td style="border-top: 1px solid black;">0.25</td> </tr> <tr> <td style="padding-right: 12px; border: none;">Adj. R<sup style="vertical-align: 0px;">2</sup></td> <td style="padding-right: 12px; border: none;">0.03</td> <td style="padding-right: 12px; border: none;">0.08</td> <td style="padding-right: 12px; border: none;">0.25</td> <td style="padding-right: 12px; border: none;">0.25</td> </tr> <tr> <td style="padding-right: 12px; border: none;">Num. obs.</td> <td style="padding-right: 12px; border: none;">534</td> <td style="padding-right: 12px; border: none;">534</td> <td style="padding-right: 12px; border: none;">534</td> <td style="padding-right: 12px; border: none;">534</td> </tr> <tr> <td style="border-bottom: 2px solid black;">RMSE</td> <td style="border-bottom: 2px solid black;">5.06</td> <td style="border-bottom: 2px solid black;">4.94</td> <td style="border-bottom: 2px solid black;">4.45</td> <td style="border-bottom: 2px solid black;">4.46</td> </tr> <tr> <td style="padding-right: 12px; border: none;" colspan="6"><span style="font-size:0.8em"><sup style="vertical-align: 0px;">***</sup>p < 0.001, <sup style="vertical-align: 0px;">**</sup>p < 0.01, <sup style="vertical-align: 0px;">*</sup>p < 0.05</span></td> </tr> </table>
これをエディタに張り付けて、エンコードをUTF-8に設定して、拡張子をhtmlにするとhtmlファイルに変わるのだが、少しめんどくさいので、一部自動化してみた。本当は全部自動化したかったのだが、どうしても、エンコードの指定だけができなかった。今後の課題としたい。
HTMLファイルの書き出し
cat関数を使う。
先ほどのtextregでHTML形式で整形したものを格納したoutput
をoutput.html
というファイル名で書きだす。
cat(output, file="output.html")
おそらく多くの人のWindowsはShift-JIS(CP932)で動いているので、catで書きだすとShift-JISで書きだされる。
しかし、UTF-8で書きだしたいのだ。
ちなみに、Macは確かUTF-8で動いていたはずなので、Macユーザーにはあまり関係ない話である。
エンコード
RStudioのRmdファイルと同じフォルダに先ほどのoutput.html
というファイルが作成されているので、テキストエディタでエンコードを変更する。
やり方はリンク先で解説されている。
https://www.1-firststep.com/archives/2258
Windowsに標準付属のテキストアプリでも可能である。
エンコードをShift-JISからUTF-8に変更して保存する。
HTMLファイルの読み込み
出力したHTML形式のファイルはhtmltoolsパッケージで取り込む。
library(htmltools) htmltools::includeHTML("output.html")
RStudioで部分的にRunすると文字化けして表示されるが、knitをすると日本語(2バイト文字)がちゃんと表示される。
出力した表を画像にしてRStudioで読み込むという手もあるが、画像をHTMLファイルと一緒に送らないといけない。それを回避するにはPDFにして全部固めるしかなかった。しかし、今回の方法では、出力して取り込んだHTMLのコードも取り込んでしまうので、knitしたHTMLファイルだけ送ることが可能だ。今回の例だとoutput.html
は同梱しなくてもよい。