And*_*ian 7 regression r plm non-linear-regression
这里我有温度时间序列面板数据,我打算为它运行分段回归或三次样条回归.首先,我快速研究了分段回归概念及其在R in中的基本实现,SO初步了解了如何继续我的工作流程.在我的第一次尝试中,我试图通过使用splines::nsin splinespackage 来运行样条回归,但是我没有得到正确的条形图.对我来说,使用基线回归或分段回归或样条回归可以起作用.
以下是我的面板数据规范的一般情况:在下面显示的第一行是我的因变量,以自然对数项和自变量表示:平均温度,总降水量和11个温度箱以及每个箱宽(AKA,箱窗) )是3摄氏度.(< - 6,-6~-3,-3~0,...> 21).
可重复的例子:
以下是使用实际温度时间序列面板数据模拟的可重现数据:
set.seed(1) # make following random data same for everyone
dat <- data.frame(index=rep(c("dex111", "dex112", "dex113", "dex114", "dex115"),
each=30),
year=1980:2009,
region= rep(c("Berlin", "Stuttgart", "Böblingen",
"Wartburgkreis", "Eisenach"), each=30),
ln_gdp_percapita=rep(sample.int(40, 30), 5),
ln_gva_agr_perworker=rep(sample.int(45, 30), 5),
temperature=rep(sample.int(50, 30), 5),
precipitation=rep(sample.int(60, 30), 5),
bin1=rep(sample.int(32, 30), 5),
bin2=rep(sample.int(34, 30), 5),
bin3=rep(sample.int(36, 30), 5),
bin4=rep(sample.int(38, 30), 5),
bin5=rep(sample.int(40, 30), 5),
bin6=rep(sample.int(42, 30), 5),
bin7=rep(sample.int(44, 30), 5),
bin8=rep(sample.int(46, 30), 5),
bin9=rep(sample.int(48, 30), 5),
bin10=rep(sample.int(50, 30), 5),
bin11=rep(sample.int(52, 30), 5))
Run Code Online (Sandbox Code Playgroud)
请注意,除了极端温度值之外,每个箱子具有相等的温度间隔,因此每个箱子给出了在相应温度间隔内下降的天数.
更新2:回归规范:
这是我的回归规范:
区域被索引i,年份被索引t.y_it是衡量产出的指标
y_it? {ln GDP per capita, ln GVA per capita (by six sectors respectively)},?_i是一组区域固定效应,它解释了地区之间未观察到的常数差异.?_t是一组年度固定效应,可以灵活地解释共同趋势.T_it^ m is the number of days in the districti and yeart`在第m个温度箱中具有一天的平均温度.每个室内温度箱宽3℃.当我对它进行样条曲线回归时,我需要添加两个固定的方式(由年份固定并按区域固定).
新更新1:
在这里,我想完全重新定义我的意图.最近我发现了非常有趣的R包,plm它适用于面板数据.这是我使用的新解决方案plm很好地工作:
library(plm)
pdf <- pdata.frame(dat, index = c("region", "year"))
model.b <- plm(ln_gdp_percapita ~ bin1+bin2+bin3+bin4+bin5+bin6+bin7+bin8+bin9+bin10+bin11, data = pdf, model = "pooling", effect = "twoways")
library(lmtest)
coeftest(model.b)
res <- summary(model.b, cluster=c("c")) ## add standard clustered error on it
Run Code Online (Sandbox Code Playgroud)
新更新3:
summary(model.b, cluster=c("c"))$coefficients # only render coefficient estimates table
Run Code Online (Sandbox Code Playgroud)
新更新2:我的输出:
> coeftest(model.b)
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
bin1 1.7773e-04 4.8242e-04 0.3684 0.7125716
bin2 2.4031e-03 4.3999e-04 5.4617 4.823e-08 ***
bin3 7.9238e-04 3.9733e-04 1.9943 0.0461478 *
bin4 -2.0406e-05 3.7496e-04 -0.0544 0.9566001
bin5 9.9911e-04 3.6386e-04 2.7459 0.0060451 **
bin6 6.0026e-05 3.4915e-04 0.1719 0.8635032
bin7 2.5621e-04 3.0243e-04 0.8472 0.3969170
bin8 -9.5919e-04 2.7136e-04 -3.5347 0.0004099 ***
bin9 -1.8195e-04 2.5906e-04 -0.7023 0.4824958
bin10 -5.2064e-04 2.7006e-04 -1.9279 0.0538948 .
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Run Code Online (Sandbox Code Playgroud)
所需的散点图:
下面是我想要实现的散点图.这只是一个模拟的散点图,其灵感来自NBER工作论文第32页,题为温度对生产率和因子重新分配的影响:来自五十万中国制造工厂的证据 - 这里有一个非门户版本,页面方向可以在整个文件中修复从命令行运行以下命令:
pdftk w23991.pdf cat 1-31 32-37east 38-40 41east 42-44 45east 46 output w23991-oriented.pdf
期望的散点图:
在该图中,黑点线是估计回归(基线或受限样条回归)系数,并且点蓝线是基于聚类标准误差的95%置信区间.
我刚刚与论文的作者联系过,他们只是用它Excel来获得那个情节.基本上,他们只是使用Estimate95%置信区间数据的右侧和左侧来产生一个情节.我知道那种阴谋Excel很容易,但我很感兴趣R.那可行吗?任何的想法?
我想通过使用R而不是使用更具编程性的方法来渲染绘图Excel.任何聪明的举动?
前言:我根本不熟悉这个问题背后的统计数据。以下内容可能对入门有所帮助ggplot2。让我知道你的想法。
set.seed(1) # make following random data same for everyone\ndat <- data.frame(index=rep(c("dex111", "dex112", "dex113", "dex114", "dex115"), \n each=30),\n year=1980:2009,\n region= rep(c("Berlin", "Stuttgart", "B\xc3\xb6blingen", \n "Wartburgkreis", "Eisenach"), each=30),\n ln_gdp_percapita=rep(sample.int(40, 30), 5), \n ln_gva_agr_perworker=rep(sample.int(45, 30), 5),\n temperature=rep(sample.int(50, 30), 5), \n precipitation=rep(sample.int(60, 30), 5), \n bin1=rep(sample.int(32, 30), 5), \n bin2=rep(sample.int(34, 30), 5), \n bin3=rep(sample.int(36, 30), 5),\n bin4=rep(sample.int(38, 30), 5), \n bin5=rep(sample.int(40, 30), 5), \n bin6=rep(sample.int(42, 30), 5),\n bin7=rep(sample.int(44, 30), 5), \n bin8=rep(sample.int(46, 30), 5), \n bin9=rep(sample.int(48, 30), 5),\n bin10=rep(sample.int(50, 30), 5), \n bin11=rep(sample.int(52, 30), 5))\n\nlibrary(plm)\npdf <- pdata.frame(dat, index=c("region", "year"))\nmodel.b <- plm(ln_gdp_percapita ~ \n bin1+bin2+bin3+bin4+bin5+bin6+bin7+bin8+bin9+bin10+bin11,\n data=pdf, model="pooling", effect="twoways")\npdf$ln_gdp_percapita_predicted <- plm:::predict.plm(model.b, pdf)\n\nlibrary(ggplot2)\nx <- ggplot(pdf, aes(y=ln_gdp_percapita_predicted, x=temperature))+\n geom_point()+\n geom_smooth(method=lm, formula=y~x, se=TRUE, level=.95)+ # see ?geom_smooth\n ylab("ln_gdp_percapita_predicted")+\n ggtitle("ln_gdp_percapita modeled as temperature")\n\nggsave("scatter_plot_2.png")\nx\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n\n更新:
\n\n绘制一个图res(参见??coefplot参考资料获取更多信息):
res <- plm:::summary.plm(model.b, cluster=c("c"))\n\nlibrary(coefplot)\ncoefplot::coefplot(res)\nggsave("model.b.coefplot.png")\nRun Code Online (Sandbox Code Playgroud)\n\n\n