いくつか方法があるが、パッケージhavenを使う方法が比較的手軽に使えるようである。以下は覚書。
require(haven)
url <- "http://www.principlesofeconometrics.com/stata/broiler.dta"
data.df <- read_dta(url)
参考文献:
いくつか方法があるが、パッケージhavenを使う方法が比較的手軽に使えるようである。以下は覚書。
require(haven)
url <- "http://www.principlesofeconometrics.com/stata/broiler.dta"
data.df <- read_dta(url)
参考文献:
Rstuidoを使っている人限定:
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
参考文献
メモ:最初に書いた内容に間違いあったため、修正。
一年ほど前に書いたRのコード(dplyr)が動かないことがわかった。コードを追ったところ(1)dplyr::selectがMASS::selectと衝突、(2)dplyr::lagがstats::lagと衝突していることが分かった(1年前は問題なく動いたような気がするのだが・・・)。
やり方としては参考文献にあるように
select <- dplyr::select
と上書きする。
覚書1:「tidyverseを最後にロード(library(tidyverse) )しても関数の衝突は解決しない」(場合がある?)ようだ。そのため上書きするのが確実な模様。
覚書2:Tidyverseのページに衝突の話はあった。
Conflicts between the tidyverse and other packages — tidyverse_conflicts • tidyverse
tidyverse_conflicts()
(これが分かるだけで30分以上費やし、なんだか少し悔しかったので、自戒のためにブログに書いておくことにする。将来、他の誰かの役に立ちますように。)
install.packages("huxtable")
require("huxtable")
lm.res1 <- lm(mpg ~ wt , data = mtcars)
lm.res2 <- lm(mpg ~ wt + qsec, data = mtcars)
lm.hux <- huxreg(lm.res1, lm.res2, number_format = "%.4f")
lm.hux
quick_docx(lm.hux, file="lmHuxSample.docx")
以下のマニュアルを読むと結構楽しい。
https://cran.r-project.org/web/packages/huxtable/vignettes/huxreg.html
後日のメモ:huxtableのhuxregを使えばいいので、jtoolsは不要かもしれない。
install.packages("flextable")
install.packages("officer")
install.packages("huxtable")
require(“jtools”)
lm.res <- lm(mpg ~ wt + qsec, data = mtcars)
lm.summ <- summ(lm.res)
export_summs(lm.summ, to.file = "docx", file.name = "test.docx")
library(ggplot2) library(dplyr) library(broom) library(openxlsx) lm.res <- lm(mpg ~ wt + qsec, data = mtcars) lm.res %>% summary() %>% tidy() -> lm.tidy lm.res %>% summary() %>% glance() -> lm.glance wb <- createWorkbook() addWorksheet(wb, 'Sheet1') addWorksheet(wb, 'Sheet2') writeData(wb, sheet = 'Sheet1', x = lm.tidy, withFilter=F) writeData(wb, sheet = 'Sheet2', x = lm.glance, withFilter=F) saveWorkbook(wb, "lm.res.xlsx", overwrite = T)
参考文献
財政政策(政府支出)のデータ分析に関する自分用のメモ:
日本のGDP統計は正確には国民経済計算体系(System of National Accounts)といい、国際的な基準(国連が定めるSNA)に準拠している。それに対してアメリカのGDP統計はNIPA (National Income and Product Accounts)といい、SNAと似ているものの微妙に違うという(若干厄介な)存在である。
たとえば、93SNA(国連が1993年に定めたSNA)以後、日本にはGNPという名称は存在しない(その代わりにGNIを使う)が、NIPAには今でもGNPが存在する。その他にも細かな違いがあって、実はちょっと厄介である。
それ以外にもマクロ経済学で恒等式Y=C+I+G+NXを考えるときに「どのデータがGなのか?」と言われたらちょっと困る。どうやらG(政府支出, Government spending)は以下となるようだ。
日本:G=政府最終消費支出+公的固定資本形成
アメリカ:G=Government consumption expenditures and gross investment