Load Zelig and attach example data frame:
data(macro, package = "Zelig")
Estimate model:
m1 <- lm(unem ~ gdp + capmob + trade, data = macro)
Summarize regression coefficients:
summary(m1)
##
## Call:
## lm(formula = unem ~ gdp + capmob + trade, data = macro)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.3008 -2.0768 -0.3187 1.9789 7.7715
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.181294 0.450572 13.719 < 2e-16 ***
## gdp -0.323601 0.062820 -5.151 4.36e-07 ***
## capmob 1.421939 0.166443 8.543 4.22e-16 ***
## trade 0.019854 0.005606 3.542 0.000452 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.746 on 346 degrees of freedom
## Multiple R-squared: 0.2878, Adjusted R-squared: 0.2817
## F-statistic: 46.61 on 3 and 346 DF, p-value: < 2.2e-16
Set explanatory variables to their default (mean/mode) values, with high (80th percentile) and low (20th percentile) values for the trade variable:
library(smargins)
m1.sm <- smargins(m1, trade = quantile(trade, c(0.2, 0.8)))
summary(m1.sm)
## trade mean sd median lower_2.5 upper_97.5
## 1 37.29106 4.601126 0.1899049 4.599692 4.235380 4.975884
## 2 79.10131 5.428728 0.1927763 5.429714 5.041725 5.796852
Calculate first differences for the effect of high versus low trade on GDP:
summary(scompare(m1.sm, var = "trade"))
## trade mean sd median lower_2.5
## 1 37.291064 vs 79.1013138 -0.8276017 0.2361636 -0.827025 -1.279808
## upper_97.5
## 1 -0.3646216
Plot the simulated distributions:
library(ggplot2)
ggplot(m1.sm, aes(x = .smargin_qi, fill = factor(trade))) +
geom_density(alpha = 0.25)
ggplot(scompare(m1.sm, var = "trade"), aes(x = .smargin_qi, fill = factor(trade))) +
geom_density()
Estimate a model with fixed effects for each country.
m2 <- lm(unem ~ gdp + trade + capmob + country, data = macro)
Calculate expected values for each country.
m2.sm <- smargins(m2, country = unique(country))
summary(m2.sm)
## country mean sd median lower_2.5 upper_97.5
## 1 United States 6.7573279 0.5050926 6.7552306 5.7688873 7.7405828
## 2 Canada 6.1104831 0.4962683 6.1222987 5.1167550 7.0580063
## 3 United Kingdom -0.2782954 0.6619426 -0.2814295 -1.5726958 1.0255557
## 4 Netherlands 11.3502660 0.6684087 11.3578167 10.0345343 12.6918320
## 5 Belgium -1.4385509 0.5050163 -1.4398102 -2.3932719 -0.4490090
## 6 France 8.2016188 0.4152688 8.2040393 7.3824031 9.0235651
## 7 West Germany -0.1425351 0.5862575 -0.1440817 -1.2792297 0.9772773
## 8 Austria 4.6572386 0.4106122 4.6582345 3.8408052 5.4588982
## 9 Italy 5.6195152 0.3809501 5.6251392 4.8594179 6.3312632
## 10 Finland 6.9024877 0.3747685 6.9034488 6.1886121 7.6324411
## 11 Sweden 2.2218711 0.3590478 2.2204035 1.5421027 2.9351323
## 12 Norway 8.0609674 0.4301200 8.0626554 7.2200565 8.8898015
## 13 Denmark 1.3007533 0.3927762 1.3027388 0.5232714 2.0589912
## 14 Japan 10.5805621 0.4026164 10.5717316 9.8091391 11.3828478
(m2.sm.comp <- summary(scompare(m2.sm, var = "country")))
## country mean sd median
## 1 United States vs Canada -4.5929381 0.5608247 -4.5859401
## 2 United States vs United Kingdom 8.1958788 0.8641772 8.1921669
## 3 United States vs Netherlands 6.8998630 0.9592247 6.8960574
## 4 United States vs Belgium 2.1000893 0.5691606 2.1084122
## 5 United States vs France -0.1451598 0.5686462 -0.1478933
## 6 United States vs West Germany 4.5354568 0.6404833 4.5459892
## 7 United States vs Austria -0.6468448 0.7769755 -0.6335292
## 8 United States vs Italy -5.2397829 0.9483057 -5.2378230
## 9 United States vs Finland 7.5490340 0.6228822 7.5482186
## 10 United States vs Sweden -2.0911357 0.6775671 -2.0740240
## 11 United States vs Norway 6.2530183 0.7148811 6.2524121
## 12 United States vs Denmark 1.4532445 0.7414005 1.4654437
## 13 United States vs Japan -0.7920046 0.6534639 -0.7772425
## 14 Canada vs United Kingdom 3.8886120 0.5588568 3.8883269
## 15 Canada vs Netherlands -4.4700790 0.6610086 -4.4674350
## 16 Canada vs Belgium -7.0356233 1.0314437 -7.0280746
## 17 Canada vs France -6.3887786 0.8025270 -6.3804515
## 18 Canada vs West Germany -11.6285614 1.2049727 -11.6212081
## 19 Canada vs Austria 1.1602555 0.5644459 1.1657278
## 20 Canada vs Italy -8.4799142 0.9211620 -8.4853353
## 21 Canada vs Finland -0.1357603 0.5238578 -0.1393740
## 22 Canada vs Sweden -4.9355341 0.8449157 -4.9464498
## 23 Canada vs Norway -5.8978107 0.7125634 -5.8896186
## 24 Canada vs Denmark -7.1807832 0.8305336 -7.1746548
## 25 Canada vs Japan -2.5001666 0.7549802 -2.4916198
## 26 United Kingdom vs Netherlands -8.3392628 0.8772133 -8.3407664
## 27 United Kingdom vs Belgium -10.8588576 0.9066525 -10.8546577
## 28 United Kingdom vs France 6.6930274 0.6632851 6.7061754
## 29 United Kingdom vs West Germany -12.7888169 1.0551043 -12.7887535
## 30 United Kingdom vs Austria -6.0957895 0.7273580 -6.0919652
## 31 United Kingdom vs Italy -8.3410387 0.6899431 -8.3480870
## 32 United Kingdom vs Finland -3.6604220 0.6031853 -3.6697712
## 33 United Kingdom vs Sweden 1.4442909 0.5297219 1.4336590
## 34 United Kingdom vs Norway -3.1486472 0.6192591 -3.1493211
## 35 United Kingdom vs Denmark 9.6401697 0.7608610 9.6404623
## 36 United Kingdom vs Japan 8.3441539 0.8459099 8.3362150
## 37 Netherlands vs Belgium 3.5443802 0.5335123 3.5413720
## 38 Netherlands vs France 1.2991311 0.5149441 1.2965029
## 39 Netherlands vs West Germany 5.9797477 0.5557264 5.9775659
## 40 Netherlands vs Austria -2.3789433 0.5016655 -2.3768140
## 41 Netherlands vs Italy -11.4928011 1.1383951 -11.4993520
## 42 Netherlands vs Finland 1.2960158 0.5233170 1.2966663
## 43 Netherlands vs Sweden -4.7997737 0.7860724 -4.7926391
## 44 Netherlands vs Norway -7.0450229 0.7579890 -7.0374611
## 45 Netherlands vs Denmark -2.3644063 0.6783142 -2.3709636
## 46 Netherlands vs Japan -1.1378127 0.6897382 -1.1287743
## 47 Belgium vs France -0.4909679 0.5670175 -0.4919801
## 48 Belgium vs West Germany -5.7307508 0.8408754 -5.7282652
## 49 Belgium vs Austria 7.0580661 0.5728782 7.0562958
## 50 Belgium vs Italy -2.5821036 0.5893668 -2.5837132
## 51 Belgium vs Finland 5.7620504 0.6395931 5.7714441
## 52 Belgium vs Sweden 0.9622766 0.5982194 0.9597769
## 53 Belgium vs Norway -1.2829725 0.5487509 -1.2717317
## 54 Belgium vs Denmark 3.3976441 0.5019859 3.4075917
## 55 Belgium vs Japan -4.9610469 0.5788446 -4.9560901
## 56 France vs West Germany -4.4477783 0.6764263 -4.4587483
## 57 France vs Austria 2.2452491 0.5263183 2.2490969
## 58 France vs Italy -9.1283949 0.7826076 -9.1312324
## 59 France vs Finland -2.4353675 0.5725562 -2.4301669
## 60 France vs Sweden -4.6806166 0.5218668 -4.6789697
## 61 France vs Norway 1.3036395 0.5487185 1.3085977
## 62 France vs Denmark 1.9504842 0.7606375 1.9464068
## 63 France vs Japan -3.2892986 0.6273013 -3.2870107
## 64 West Germany vs Austria 9.4995183 0.7613087 9.5023415
## 65 West Germany vs Italy -0.1406514 0.5298125 -0.1401473
## 66 West Germany vs Finland 8.2035025 0.8257011 8.2085791
## 67 West Germany vs Sweden 3.4037288 0.5065563 3.4095898
## 68 West Germany vs Norway 2.4414521 0.6209696 2.4384796
## 69 West Germany vs Denmark 1.1584796 0.5258868 1.1593915
## 70 West Germany vs Japan 5.8390962 0.5904860 5.8409740
## 71 Austria vs Italy -2.5195947 0.5377892 -2.5263694
## 72 Austria vs Finland -5.4565746 0.7158540 -5.4517635
## 73 Austria vs Sweden -4.8097298 0.5646885 -4.8142026
## 74 Austria vs Norway 1.5790488 0.6719272 1.5707560
## 75 Austria vs Denmark -10.0495127 0.8863831 -10.0425727
## 76 Austria vs Japan 2.7393043 0.5429977 2.7508861
## 77 Italy vs Finland -6.9008655 0.6265077 -6.8970345
## 78 Italy vs Sweden 1.4432885 0.6037540 1.4319940
## 79 Italy vs Norway -3.3564853 0.6192298 -3.3717558
## 80 Italy vs Denmark -4.3187619 0.5114733 -4.3198323
## 81 Italy vs Japan -5.6017344 0.5753004 -5.5907156
## 82 Finland vs Sweden -0.9211178 0.5094879 -0.9097592
## 83 Finland vs Norway -6.7602140 0.6457727 -6.7620039
## 84 Finland vs Denmark -9.2798088 0.6058689 -9.2833014
## 85 Finland vs Japan 3.8232342 0.5389141 3.8204211
## 86 Sweden vs Norway -0.7697039 0.6405660 -0.7709777
## 87 Sweden vs Denmark 12.0191130 0.7430620 12.0173452
## 88 Sweden vs Japan 10.7230972 0.8286984 10.7157921
## 89 Norway vs Denmark 5.9233235 0.5334300 5.9102650
## 90 Norway vs Japan 3.6780744 0.5109673 3.6730251
## 91 Denmark vs Japan 8.3586910 0.5420914 8.3559075
## lower_2.5 upper_97.5
## 1 -5.68779497 -3.51809722
## 2 6.52769634 9.88367149
## 3 5.02761624 8.75670556
## 4 0.97066802 3.20897041
## 5 -1.24388902 0.96878542
## 6 3.29329682 5.77730951
## 7 -2.18825233 0.84838141
## 8 -7.12488164 -3.39353590
## 9 6.32935603 8.78509543
## 10 -3.44218277 -0.78306266
## 11 4.87482684 7.68072962
## 12 -0.01993261 2.90510956
## 13 -2.08387441 0.46691480
## 14 2.77961455 4.97659041
## 15 -5.79720387 -3.18380264
## 16 -9.06601739 -5.05434140
## 17 -7.98934824 -4.84335710
## 18 -14.00515752 -9.28629063
## 19 0.05143437 2.24723800
## 20 -10.25398089 -6.68353637
## 21 -1.14873107 0.91789937
## 22 -6.58477357 -3.25165927
## 23 -7.31170433 -4.51574294
## 24 -8.85313960 -5.51749785
## 25 -4.00690010 -1.03314760
## 26 -10.01382052 -6.60795039
## 27 -12.67241729 -9.06214177
## 28 5.36749769 7.97756317
## 29 -14.88622558 -10.70354994
## 30 -7.51489592 -4.68253517
## 31 -9.67413870 -7.01124053
## 32 -4.85042243 -2.49323678
## 33 0.41873293 2.47744225
## 34 -4.36608713 -1.94871437
## 35 8.14869589 11.14817051
## 36 6.71811049 9.98459333
## 37 2.52214653 4.58744889
## 38 0.27231259 2.29630186
## 39 4.90794162 7.09066241
## 40 -3.34661339 -1.38657218
## 41 -13.72828830 -9.29606104
## 42 0.26009096 2.34286945
## 43 -6.35034170 -3.26350681
## 44 -8.53419758 -5.56251081
## 45 -3.68382572 -1.05184861
## 46 -2.52603546 0.19749501
## 47 -1.61158873 0.61355244
## 48 -7.40560308 -4.11801368
## 49 5.90429804 8.18769418
## 50 -3.74750242 -1.45430879
## 51 4.49508173 7.00766634
## 52 -0.20051827 2.13601614
## 53 -2.34364148 -0.21171658
## 54 2.39466347 4.34903954
## 55 -6.13750146 -3.83648096
## 56 -5.77318201 -3.10218990
## 57 1.21025258 3.27209905
## 58 -10.66134475 -7.59646293
## 59 -3.54501087 -1.30558639
## 60 -5.71209065 -3.64523406
## 61 0.21259861 2.38180926
## 62 0.46018696 3.45052799
## 63 -4.54963288 -2.07815767
## 64 7.98423538 10.97527219
## 65 -1.17072875 0.88973679
## 66 6.57724847 9.78931298
## 67 2.40910216 4.40952328
## 68 1.26194739 3.65218880
## 69 0.11255818 2.16710475
## 70 4.65481481 7.00936453
## 71 -3.58621921 -1.45078047
## 72 -6.85964146 -4.03987435
## 73 -5.92090323 -3.70068280
## 74 0.29193127 2.89192106
## 75 -11.79613646 -8.29053921
## 76 1.67159868 3.78521510
## 77 -8.12099840 -5.68534850
## 78 0.26729646 2.63330661
## 79 -4.55308272 -2.10358281
## 80 -5.32279935 -3.32578583
## 81 -6.74119406 -4.48581041
## 82 -1.91730477 0.04850384
## 83 -8.03477050 -5.49147484
## 84 -10.47390637 -8.13385957
## 85 2.79150305 4.87197761
## 86 -2.00916498 0.47000589
## 87 10.58409019 13.45420144
## 88 9.11722786 12.34143882
## 89 4.86130752 6.98212497
## 90 2.67195919 4.68256037
## 91 7.29883650 9.41504581
Plot.
m2.sm.comp$country <- reorder(m2.sm.comp$country, m2.sm.comp$median)
ggplot(m2.sm.comp, aes(y = country, x = median)) +
geom_point() +
geom_errorbarh(aes(xmin = lower_2.5, xmax = upper_97.5), height = .2)