我一直在绘制预测值,这些预测值会改变我的焦点独立变量。但是,我注意到我的焦点变量(x)的系数为负,但预测曲线显示为正斜率。下面是一个足够小而不会产生问题的示例。我对示例数据表示歉意,我能得到的最小值是N = 250,同时减少了原始模型中的协变量。有四个变量:y,x,ctry(用于国家/地区固定效果)和year(用于时间固定效果)。我使用一个简单的OLS模型。
df <- structure(list(ctry = structure(c(45L, 25L, 117L, 41L, 111L,
106L, 113L, 73L, 137L, 18L, 97L, 145L, 89L, 91L, 131L, 51L, 70L,
123L, 28L, 129L, 26L, 41L, 34L, 17L, 18L, 6L, 135L, 104L, 36L,
50L, 120L, 107L, 128L, 132L, 44L, 148L, 81L, 39L, 37L, 16L, 133L,
136L, 15L, 127L, 114L, 30L, 108L, 77L, 75L, 109L, 116L, 103L,
143L, 36L, 143L, 78L, 124L, 43L, 81L, 107L, 126L, 54L, 127L,
130L, 101L, 66L, 100L, 23L, 40L, 75L, 11L, 85L, 67L, 31L, 57L,
55L, 57L, 67L, 95L, 63L, 52L, 140L, 46L, 94L, 69L, 147L, 40L,
129L, 60L, 7L, 98L, 97L, 125L, 144L, 110L, 100L, 128L, 20L, 53L,
27L, 38L, 44L, 112L, 12L, 5L, 107L, 48L, 3L, 72L, 53L, 33L, 48L,
138L, 58L, 7L, 56L, 39L, 102L, 130L, 35L, 29L, 121L, 80L, 113L,
119L, 143L, 1L, 8L, 64L, 124L, 141L, 63L, 27L, 28L, 143L, 22L,
82L, 147L, 62L, 146L, 12L, 24L, 71L, 84L, 118L, 136L, 10L, 22L,
61L, 102L, 88L, 66L, 47L, 136L, 86L, 27L, 2L, 113L, 141L, 137L,
80L, 108L, 65L, 49L, 90L, 4L, 59L, 93L, 54L, 32L, 2L, 141L, 11L,
11L, 84L, 78L, 16L, 102L, 42L, 77L, 63L, 24L, 122L, 101L, 13L,
1L, 9L, 59L, 60L, 104L, 105L, 87L, 20L, 116L, 115L, 130L, 17L,
107L, 39L, 95L, 76L, 131L, 134L, 55L, 52L, 129L, 82L, 99L, 41L,
61L, 56L, 145L, 40L, 92L, 43L, 107L, 13L, 14L, 21L, 29L, 74L,
96L, 115L, 83L, 73L, 19L, 118L, 140L, 142L, 68L, 50L, 32L, 72L,
55L, 91L, 126L, 70L, 1L, 79L, 2L, 139L, 125L, 49L, 132L, 26L,
44L, 54L, 65L, 14L, 15L), .Label = c("ADO", "AFG", "AGO", "ALB",
"ARE", "ARG", "ARM", "ATG", "AZE", "BDI", "BEL", "BEN", "BFA",
"BGD", "BGR", "BHS", "BIH", "BLR", "BLZ", "BRA", "BRB", "BRN",
"BWA", "CAF", "CHL", "CHN", "CIV", "CMR", "COG", "COL", "COM",
"CPV", "CRI", "CUB", "CYP", "CZE", "DJI", "DMA", "DOM", "DZA",
"ECU", "EST", "ETH", "FJI", "FSM", "GAB", "GEO", "GHA", "GIN",
"GMB", "GNQ", "GRD", "GTM", "GUY", "HKG", "HND", "HRV", "HTI",
"HUN", "IND", "IRN", "ISL", "ISR", "JAM", "JPN", "KAZ", "KHM",
"KIR", "KNA", "KOR", "KWT", "LAO", "LBN", "LBY", "LCA", "LIE",
"LKA", "LSO", "LVA", "MAC", "MAR", "MCO", "MDA", "MDG", "MDV",
"MEX", "MHL", "MKD", "MLI", "MMR", "MUS", "MYS", "NER", "NGA",
"NIC", "NPL", "NRU", "PAK", "PAN", "PER", "PLW", "PNG", "POL",
"PRY", "QAT", "ROM", "RUS", "RWA", "SAU", "SDN", "SEN", "SGP",
"SLB", "SLE", "SLV", "SMR", "SOM", "SRB", "STP", "SVK", "SVN",
"SWZ", "SYC", "TCD", "TGO", "THA", "TJK", "TKM", "TMP", "TON",
"TTO", "TUN", "TUR", "TUV", "TWN", "TZA", "UGA", "URY", "UZB",
"VCT", "VEN", "VNM", "WBG", "WSM", "YEM", "ZAF", "ZMB", "ZWE"
), class = "factor"), year = c(2005L, 2005L, 2000L, 2000L, 2010L,
2000L, 2000L, 2005L, 2010L, 2010L, 2010L, 2010L, 2005L, 2000L,
2005L, 2000L, 2000L, 2000L, 2010L, 2005L, 2010L, 2005L, 2000L,
2005L, 2000L, 2010L, 2010L, 2010L, 2010L, 2010L, 2000L, 2000L,
2010L, 2000L, 2010L, 2000L, 2000L, 2000L, 2000L, 2005L, 2005L,
2005L, 2010L, 2005L, 2000L, 2000L, 2005L, 2000L, 2005L, 2000L,
2010L, 2010L, 2005L, 2005L, 2005L, 2000L, 2005L, 2010L, 2005L,
2005L, 2010L, 2005L, 2010L, 2000L, 2000L, 2010L, 2010L, 2000L,
2000L, 2000L, 2010L, 2010L, 2000L, 2005L, 2000L, 2005L, 2010L,
2010L, 2000L, 2010L, 2005L, 2000L, 2005L, 2005L, 2010L, 2010L,
2010L, 2010L, 2005L, 2010L, 2000L, 2005L, 2010L, 2000L, 2000L,
2005L, 2005L, 2010L, 2000L, 2005L, 2010L, 2000L, 2005L, 2000L,
2010L, 2005L, 2000L, 2005L, 2000L, 2005L, 2000L, 2005L, 2000L,
2000L, 2005L, 2010L, 2005L, 2005L, 2010L, 2010L, 2000L, 2010L,
2005L, 2005L, 2000L, 2010L, 2005L, 2005L, 2000L, 2010L, 2005L,
2005L, 2010L, 2005L, 2010L, 2000L, 2010L, 2005L, 2005L, 2000L,
2005L, 2010L, 2000L, 2010L, 2000L, 2010L, 2000L, 2010L, 2010L,
2010L, 2005L, 2000L, 2010L, 2000L, 2005L, 2000L, 2010L, 2010L,
2010L, 2005L, 2000L, 2000L, 2000L, 2010L, 2000L, 2010L, 2010L,
2010L, 2010L, 2010L, 2005L, 2000L, 2000L, 2005L, 2005L, 2005L,
2010L, 2000L, 2000L, 2010L, 2000L, 2000L, 2000L, 2005L, 2000L,
2000L, 2010L, 2000L, 2010L, 2000L, 2010L, 2010L, 2000L, 2000L,
2010L, 2005L, 2010L, 2010L, 2010L, 2005L, 2005L, 2010L, 2005L,
2000L, 2010L, 2000L, 2000L, 2000L, 2010L, 2000L, 2000L, 2000L,
2005L, 2010L, 2000L, 2000L, 2010L, 2005L, 2000L, 2005L, 2000L,
2005L, 2005L, 2005L, 2010L, 2010L, 2005L, 2010L, 2000L, 2010L,
2000L, 2000L, 2005L, 2005L, 2005L, 2000L, 2010L, 2010L, 2010L,
2000L, 2010L, 2000L, 2000L, 2005L, 2000L, 2005L, 2000L, 2005L,
2010L, 2000L), y = c(-0.6733445533, 0.8285518176, 1.1568811968,
1.9329696378, 1.1281710909, 0.9122827105, -0.0304592075, 2.5771819259,
0.0198026273, 0.3435897044, 2.3878449369, -0.6539264674, -0.0304592075,
2.3194422101, 3.2854123487, 1.8764069433, 0.8754687374, 2.5463152779,
0.3074846997, 1.1505720276, -1.30933332, 2.2690283095, 2.238579763,
2.9932291433, -0.2876820725, 0.7747271676, 0.9282193027, 1.0473189943,
1.2149127444, 1.6193882433, 1.0715836163, -1.5141277326, -1.8325814637,
1.5260563035, 3.1333179365, 0.3074846997, 1.9430489168, 2.427454075,
-0.5447271754, 2.4449523343, 1.6213664833, -0.6348782724, 1.7281094422,
-0.7765287895, 0.6881346387, 1.0612565021, -1.347073648, 0.9707789172,
3.0951250174, -1.6607312068, 1.8164520818, 1.6639260977, 1.0715836163,
1.1410330046, 1.0715836163, -2.302585093, -2.302585093, -0.4620354596,
2.0554049639, -1.0788096614, -0.3147107448, 3.8705758155, -0.7550225843,
3.8048831299, 1.7334238922, 0.1133286853, 1.4255150743, -1.237874356,
1.4678743481, 3.1166215908, 1.4206957878, -0.7133498879, 1.2809338455,
1.393766376, 2.8367365421, 2.2202898503, 2.6411978941, 1.2809338455,
1.986503546, 1.2974631474, 3.8514233833, 3.5717836822, -0.2107210313,
-0.5621189182, 3.7235223976, -0.0943106795, 1.4109869737, 1.1151415906,
-0.8439700703, 1.9006138741, -0.0304592075, 2.2985770716, 0.1655144385,
3.8068846873, -1.30933332, 1.2919836816, -1.9661128564, -0.5978370008,
1.9198594719, -0.7550225843, 3.9164133537, 3.024805521, 1.2296405511,
-0.9942522733, -0.1278333715, -1.0788096614, 0.6312717768, 1.3609765531,
2.17702187, 2.2834022736, 0.9477893989, 0.7701082217, 1.2267122913,
2.2428350886, 1.7561322916, 2.4318574287, 2.6041700706, -0.0833816089,
3.9025785466, 2.7511096906, 1.0296194172, 1.800058272, 1.4838746895,
-0.1278333715, 2.8100049236, 1.064710737, 1.913977102, 3.4747575848,
3.4793922292, -2.0402208285, 0.5247285289, 1.32175584, -0.1625189295,
-0.1053605157, 1.064710737, 1.2267122913, 2.1949998823, -0.1984509387,
2.2417729536, 0.2546422184, -1.0216512475, -0.5798184953, 0.6418538862,
-0.4620354596, 1.6467336972, -0.5621189182, -1.4696759701, 1.3029127522,
0.5007752879, -0.1165338163, 2.5128460185, -0.3147107448, 1.5260563035,
-0.6348782724, 2.6019486702, -0.7550225843, 0.4382549309, -0.1743533871,
0.6043159669, -0.0100503359, 1.5303947051, -1.4696759701, -0.7339691751,
0.1823215568, -1.5141277326, 2.8964642719, 1.4469189829, -2.4079456087,
3.8871153697, 3.5157158351, 0.4510756194, 0.3784364357, 1.2753628004,
1.3480731483, -0.7133498879, -2.1202635362, 2.446685437, -0.0512932944,
2.5201129055, 1.0715836163, 1.1662709371, -1.0788096614, 1.2837077723,
2.0373166154, -2.2072749132, 1.887069649, -0.3011050928, 1.3137236683,
-0.7765287895, -0.3147107448, -1.6607312068, 0.9707789172, -1.1086626245,
1.6770965609, 3.2827891506, 3.8454557612, 2.9785861147, -0.9162907319,
2.6546494244, 2.1246538845, 2.48740353, 3.2763897311, 2.8604851241,
2.1459312829, 3.8753590211, 1.2499017362, 2.05796251, 1.8640801308,
2.302585093, 0.5933268453, 1.9459101491, -0.8675005677, 1.3110318766,
0.4510756194, -0.8439700703, -1.5141277326, -1.9661128564, -0.8439700703,
3.4045251718, 1.0152306797, -0.2613647641, -1.2729656758, 3.2047769005,
0.955511445, 2.5945081597, 3.1523085806, 2.1198634562, 3.6256733782,
1.0473189943, 1.4996230464, 1.3110318766, 3.4830845411, 2.2617630985,
2.2202898503, 2.3045830957, -0.7339691751, 1.1442227999, 1.9444805562,
1.8748743759, 0.3435897044, -0.2613647641, 0.0953101798, -0.8209805521,
1.3887912413, -1.7147984281, 3.0869431536, 3.7203781212, -0.7339691751,
-0.7985076962, 0.8372475245), x = c(0.520716395, 0.76448135,
0.1859026067, 0.4139582267, 0.455963932, 0.56432366, 0.6416624933,
0.444074425, 0.41868816, 0.265443, 0.62583766, 0.286851672, 0.461673335,
0.5781655933, 0.56065131, 0.26364296, 0.4985219733, 0.55528988,
0.26919532, 0.412561715, 0.396095096, 0.3577011, 0.3840438467,
0.379925565, 0.4366703067, 0.377308448, 0.67303668, 0.310457824,
0.680926444, 0.421162232, 0.64763204, 0.4086114733, 0.189257,
0.4532333267, 0.418217804, 0.3818111133, 0.5274238733, 0.4740385933,
0.5051548067, 0.7800376, 0.511907415, 0.435252965, 0.484772744,
0.2611265, 0.3427766933, 0.39252946, 0.31492562, 0.5823207, 0.640646835,
0.4660631067, 0.681284708, 0.599554924, 0.46166572, 0.65774412,
0.46166572, 0.46239142, 0.25585435, 0.349241016, 0.50003636,
0.313353075, 0.480026752, 0.39368275, 0.25535868, 0.5203671933,
0.55919694, 0.32805816, 0.36327362, 0.5645227667, 0.3182517,
0.6154360733, 0.76258512, 0.477428292, 0.2505129333, 0.29323284,
0.3891413067, 0.76361295, 0.51883124, 0.26491668, 0.5506494467,
0.6694268, 0.57734314, 0.61555678, 0.434264495, 0.23289585, 0.655019476,
0.40125994, 0.349313036, 0.299113012, 0.525231765, 0.409542412,
0.43688136, 0.67115676, 0.313869372, 0.612537, 0.3008297133,
0.391574355, 0.2102851, 0.434452664, 0.4266350667, 0.23922155,
0.632421328, 0.5101546533, 0.7967896, 0.4488204, 0.573298208,
0.313353075, 0.5731814333, 0.18508375, 0.2960349133, 0.303098115,
0.63644668, 0.50734459, 0.6924114933, 0.29922598, 0.41864991,
0.317488556, 0.371810575, 0.308939985, 0.562287792, 0.721651452,
0.2128437333, 0.692473748, 0.67843838, 0.33718355, 0.50659921,
0.403681844, 0.7430906, 0.705581225, 0.48459576, 0.19086236,
0.273353275, 0.696210505, 0.2184086, 0.2654355, 0.403681844,
0.58629872, 0.681284708, 0.41636569, 0.88027415, 0.3658443867,
0.436338235, 0.2088088, 0.5567543467, 0.405381548, 0.31935366,
0.423064352, 0.2940382933, 0.5812443, 0.314591584, 0.320143128,
0.407616765, 0.37066398, 0.420301544, 0.3951682467, 0.430066195,
0.4198998133, 0.1374802, 0.359664432, 0.20068, 0.374058295, 0.6433970933,
0.2563798867, 0.6655147467, 0.2084578, 0.2244902, 0.373739, 0.681972528,
0.378276344, 0.386861176, 0.61182554, 0.1649884, 0.4262007667,
0.6226592067, 0.7703788, 0.448730595, 0.51076983, 0.728919716,
0.4223420333, 0.4807863267, 0.513546168, 0.7561993333, 0.3969347333,
0.42510414, 0.67115676, 0.3854491267, 0.7466545333, 0.329258048,
0.73184386, 0.519955152, 0.3475113867, 0.633509884, 0.494471196,
0.5069802867, 0.66498058, 0.374557092, 0.509559555, 0.40981766,
0.31671136, 0.368289476, 0.35624449, 0.77207405, 0.472227684,
0.745716025, 0.54377252, 0.536809976, 0.2916619867, 0.57879064,
0.5064780533, 0.288768148, 0.3688028467, 0.3102560733, 0.29936902,
0.34749761, 0.58812174, 0.4653794733, 0.4086114733, 0.423902084,
0.305882855, 0.6150655867, 0.248726, 0.2824005133, 0.3989114,
0.39564993, 0.403281625, 0.379191, 0.464322504, 0.314557485,
0.661148164, 0.2923557, 0.612319748, 0.4584805133, 0.7473852,
0.273135885, 0.76361295, 0.70575823, 0.5448304667, 0.69060902,
0.717824748, 0.646824664, 0.1474785, 0.23526096, 0.32274084,
0.2544196667, 0.47831574, 0.5081830467, 0.46158542, 0.36767402,
0.7477056, 0.330186, 0.5044399933)), .Names = c("ctry", "year",
"y", "x"), row.names = c(NA, -250L), class = "data.frame")
下面是模型和结果(省略年份并尝试固定效果):
m <- lm(y ~ x + factor(year) + factor(ctry) , data=df)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.15066 0.22311 9.640 6.69e-16 ***
x -0.48362 0.26969 -1.793 0.075988 .
然后我继续用预测值进行绘图。
plot.df <- cbind(df, predict(m, newdata = df, se.fit = T, interval='confidence'))
library(ggplot2)
ggplot(data = plot.df, aes(x = x, y = fit.fit)) +
stat_smooth(method=lm, se=T, color='red',data = plot.df, level=0.95, aes(x=x, y=fit.fit))
我为什么在回归中系数为负而在预测图中斜率为正?我觉得这与国家固定效应有关。当我在没有国家固定影响的情况下运行模型时,我得到一个正系数。预测图如何反映负系数?感谢您的时间。
答案 0 :(得分:1)
您需要使模型中的所有其他变量“保持不变”,以查看x
与y
的负偏关系:
new.x <- seq(0.25, 0.75, 0.01)
pred.y <- predict(m, newdata=data.frame(x=new.x, year="2005", ctry="AGO"))
plot(x=new.x, y=pred.y, pch=20)
看到正相关关系的原因是,当您进行predict()
时,您使用的是数据集中的任何数据,特别是其中和< / em>您具有的年份和国家/地区值。必须将x
的较高值与具有正系数的国家或年份相关联,以抵消x
的负面影响。
此现象的一般版本称为Simpson's Paradox。