【发布时间】:2019-05-22 15:52:02
【问题描述】:
我想知道我是否可以在这个数据框上运行多个回归:
Country Years FDI_InFlow_MilUSD FDI_InFlow_percGDP FDI_InStock_MilUSD FDI_OutFlow_MilUSD FDI_OutFlow_percGDP
1 Netherlands 1990 11063.31 3.52 71827.79 14371.94 34.96
2 Romania 1990 0.01 0.00 0.01 18.00 0.16
3 Netherlands 1991 6074.61 1.88 75404.38 13484.54 37.09
4 Romania 1991 40.00 0.13 44.00 3.00 0.29
5 Netherlands 1992 6392.10 1.78 73918.54 13153.78 33.15
6 Romania 1992 77.00 0.37 122.00 4.00 0.38
在这种情况下,我想对所有感兴趣的变量进行 3:7 的回归(我的原始数据有 10 个变量,但我认为这足以说明我想要的)。此外,我希望将 lm 结果存储在数据框中并按国家/地区分组(如果可能的话),而不是为每个国家/地区制作 2 个 dfs,然后遍历它们..
这是一个想要的df的例子(这个没有分组):
# term estimate std.error statistic p.value
# 1 (Intercept) -3.2002150 0.256885790 -12.457735 8.141394e-25
# 2 Sepal.Length 0.7529176 0.043530170 17.296454 2.325498e-37
# 3 (Intercept) 3.1568723 0.413081984 7.642242 2.474053e-12
# 4 Sepal.Width -0.6402766 0.133768277 -4.786461 4.073229e-06
# 5 (Intercept) -0.3630755 0.039761990 -9.131221 4.699798e-16
# 6 Petal.Length 0.4157554 0.009582436 43.387237 4.675004e-86
这是期望结果的示例:在这种情况下,计算是针对两个国家的,并且每个国家只分配两次
Country term estimate std.error statistic p.value
1 Netherlands (Intercept) -67825.16741 2.229068e+04 -3.042759 3.615586e-03
2 Netherlands GDP_pcap_USD 14.04734 7.908839e-01 17.761576 3.285528e-24
3 Romania (Intercept) -67825.16741 2.229068e+04 -3.042759 3.615586e-03
4 Romania GDP_pcap_USD 14.04734 7.908839e-01 17.761576 3.285528e-24
我使用了这行代码:FDI2 %>% group_by(Country) %>% do(tidy(lm(FDI_InStock_MilUSD ~ GDP_pcap_USD, data= FDI2)))
【问题讨论】:
标签: r loops statistics regression linear-regression