I've been trying to plot a 95% prediction interval for a multivariate regression line in ggplot2. The graph is a regression of three independent variables ("x", "y", and "z") being used to predict a dependent variable ("a") on the y-axis. However, when I actually try to plot the results in ggplot2, I get a rather unusual result where the regression line is straightforward but the 95% prediction interval bands are very squiggly and do not resemble a straight line at all. They look like loess lines more than anything. Here is a picture showing the result I get:
Does anyone know why I am getting a result where the 95% confidence intervals aren't smooth lines? The only thing I can think of is that this is related to the fact that this is a multivariate regression rather than a univariate one, but checking the actual variables all three show a strong correlation with the dependent variable (r2 > 0.95). I looked up the results of a plot of a multivariate regression with a 95% confidence interval, but none of them seemed similar to my result, they all seemed to have pretty smooth lines.
I tried fitting a method="lm"
into the predict()
call of my code following this question, but that did not work either.
Below is a dataset and code that replicates this result.
x y z a
1 2.366153239 5.420534999 2.328204243 10.55858156
2 1.431094272 2.975529566 1.724972338 2.533696814
3 2.60453538 5.75827066 2.399639694 11.48783737
4 2.483771412 5.470167623 2.338838948 10.74706177
5 1.971210737 4.287715955 2.070680071 7.334766592
6 2.5596573 5.558000525 2.357541203 11.6127708
7 2.177892158 4.730480377 2.174966753 8.631949429
8 1.49665751 3.203559121 1.78984891 3.020424886
9 2.728865195 6.376658918 2.525204728 12.51412704
10 1.908668224 4.025351691 2.006327912 6.593044534
11 1.978895443 4.24563401 2.060493633 7.402451521
12 1.627855104 3.344274234 1.828735693 3.731699451
13 1.53436705 3.350605596 1.83046595 3.170525564
14 2.448831586 5.585936937 2.363458681 10.76866329
15 2.443160968 5.331752143 2.309058714 10.58310613
16 2.156078216 4.417635062 2.101817086 8.109576771
17 1.931534652 4.249610334 2.061458303 6.790693233
18 1.452715015 3.225752129 1.796037897 3.356200016
19 1.729354145 3.683866912 1.919340228 5.420225217
20 1.861239059 3.912023005 1.977883466 6.267750682
21 1.822955174 3.804437795 1.950496807 5.991464547
22 2.113126565 4.492001488 2.119434238 8.114076324
23 2.171856126 4.662613282 2.159308519 7.806138626
24 1.391215895 3.010620886 1.735114084 2.461296784
25 1.319165859 2.895911938 1.701737917 2.055404964
26 2.034006688 4.322608316 2.079088338 6.977001452
27 2.85574569 6.160996329 2.482135437 14.34613881
28 1.411579618 3.097385927 1.759939183 2.613006652
29 2.576957482 6.029643051 2.45553315 11.91628836
30 1.796913834 3.923259637 1.980721999 5.911392672
31 2.024389004 4.345833727 2.084666335 8.022132643
32 1.63435577 3.493472658 1.869083374 3.515715835
33 1.584595569 3.453157121 1.858267236 3.397523976
34 1.881578895 4.030076005 2.00750492 6.011267174
35 1.728309802 3.752101123 1.937034105 5.225370259
36 1.414715557 3.140049044 1.772018353 2.736961545
37 1.488730081 3.116621591 1.76539559 2.902519892
38 1.522138034 3.257327011 1.804806641 2.890371758
39 1.800033345 3.987130478 1.996780027 5.640594153
40 1.794222122 4.143928062 2.035664035 6.206575927
41 2.676710091 6.289901082 2.50796752 13.49805633
42 2.328582719 5.13691546 2.266476442 9.430961545
43 2.484723966 5.458712793 2.336388836 10.7561993
44 2.287108375 4.856940066 2.203846652 9.917240545
45 2.417128932 5.582744146 2.362783136 10.54534144
46 2.328332495 5.105945474 2.259633925 9.840475333
47 2.362264634 5.293304825 2.300718328 9.848820151
48 2.28292536 5.018934097 2.24029777 9.269934816
49 1.449825221 3.006177531 1.73383319 3.121042465
50 2.211679876 4.692264893 2.166163635 8.631218063
51 2.704614597 6.072756474 2.464296345 12.31992499
52 2.48097622 5.43590303 2.331502312 11.2245765
53 1.497529983 3.380994674 1.838748127 3.752088968
54 2.696365396 5.825540285 2.413615604 12.36222133
55 2.165729837 4.666265285 2.160153996 8.455875079
56 2.410978268 5.417499423 2.327552239 10.08813972
57 2.185447829 4.991792206 2.234231905 9.215327913
58 2.041898307 4.22566518 2.055642279 7.418180823
59 2.099077244 4.375757022 2.091831021 7.696212639
60 2.000032635 4.234467391 2.057782153 7.110696123
61 2.025963678 4.260852439 2.064183238 6.851163763
62 2.083395224 4.351567427 2.08604109 7.884576511
63 1.981523362 4.318820559 2.07817722 7.43543802
64 2.033235038 4.336636932 2.082459347 7.313220387
65 1.423999144 3.206803244 1.790754937 2.564949357
66 2.217982257 4.825910853 2.196795587 8.920558764
67 1.240285111 2.808498672 1.675857593 1.568615918
68 2.215837149 5.041487758 2.245325758 8.802372134
69 2.134859238 4.731890939 2.175291001 8.132101136
70 2.306998207 5.059171458 2.249260202 9.336074756
71 1.896404791 4.104681782 2.026001427 6.445449942
72 1.922935417 4.151905673 2.037622554 6.818169682
73 2.111422924 4.716264233 2.171696165 8.366370302
74 2.28264494 4.852811209 2.202909714 9.210340372
75 2.190760504 4.574710979 2.1388574 8.447427164
76 2.037589062 4.275276265 2.06767412 6.989197008
77 1.717192759 3.810543836 1.952061433 4.610157727
78 1.876769266 4.043051268 2.010734012 6.306275287
79 2.030134158 4.579339426 2.139939117 7.715792425
80 1.93577016 4.356708827 2.08727306 6.788521191
81 2.056518774 4.445588116 2.108456335 7.636510887
82 2.120080841 4.615120517 2.148283156 7.916807491
83 2.232689054 4.861361591 2.204849562 8.694167142
84 2.181147406 4.782479201 2.186888017 8.854567878
85 2.92779884 6.305666829 2.511108685 13.9593635
86 1.860080456 4.459637473 2.111785376 6.163314804
87 1.913818428 4.602767301 2.145406092 7.174915716
88 1.877883958 4.594104966 2.143386332 6.335054251
89 1.994987686 4.632100752 2.152231575 7.707952547
90 2.14756511 5.023880521 2.241401464 9.161721393
91 1.503591471 3.687628672 1.92031994 4.280824129
92 1.4536743 3.579343567 1.891915317 3.761200116
93 1.50872427 3.584888833 1.893380266 4.106767082
94 1.537573733 3.649466946 1.910357806 4.126327608
95 1.934796461 4.373238129 2.091228856 7.584097036
96 1.526250724 3.248434627 1.802341429 3.228826156
97 1.606399474 3.500439216 1.870946075 4.939855112
98 1.943162189 4.329208633 2.080675043 6.460498957
99 1.963384107 4.353112625 2.086411423 6.649308332
100 2.183124049 4.711248626 2.170541091 8.474527832
101 1.640763809 3.543853682 1.882512598 3.832330237
102 1.659456682 3.523415014 1.877076188 3.997282849
103 1.436096958 3.166318574 1.779415234 2.839078464
104 2.428955194 4.91133048 2.216152179 10.44793169
105 2.668500746 6.154858094 2.480898646 12.73883098
106 2.676812229 6.178980921 2.485755604 12.64109656
107 2.126920019 4.640923356 2.154280241 8.600833727
108 1.878254881 4.025530246 2.00637241 6.253828812
109 2.242102174 4.726797674 2.174119977 8.29404964
110 1.676813632 3.822754538 1.955186574 5.370638028
111 1.874531192 4.17438727 2.043131731 7.265087007
112 1.998637301 4.2363594 2.058241822 6.722389092
113 1.944116978 4.159527009 2.039491851 6.038562805
114 2.308184503 5.192956851 2.278806014 9.36048303
115 2.042370888 4.49535532 2.120225299 7.320526962
116 2.015621187 4.318820559 2.07817722 7.081078135
117 1.81401665 4.146304301 2.036247603 6.492542819
118 1.676813632 3.87937827 1.969613736 5.221868194
119 2.807346477 6.428545769 2.535457704 13.72308897
120 1.621259207 3.543853682 1.882512598 4.162470391
121 1.50100345 3.321793359 1.822578766 3.106378794
122 1.582428764 3.464319806 1.861268333 4.143134726
123 1.654547625 3.591817741 1.895209155 4.509649984
124 2.332936461 4.937777822 2.222111118 9.398917323
125 2.498105588 5.513601542 2.348105948 11.29414737
126 1.890319403 3.887730313 1.97173282 5.847161058
127 1.804890841 3.940999114 1.985194981 6.17864926
128 2.096209309 4.6042388 2.145749007 7.788418833
129 2.047658751 4.337290741 2.082616321 7.612336837
130 2.680572077 5.989462544 2.447337848 12.15745472
131 2.333554566 5.407171771 2.325332615 10.44467195
132 2.212180997 4.932817886 2.220994797 8.881836305
133 1.478852439 3.063390922 1.750254531 2.890371758
134 1.648334702 3.518387649 1.875736562 4.141546164
135 2.307921185 4.90823336 2.215453308 9.305650552
136 2.13384989 4.645130271 2.155256428 8.018790088
137 1.728309802 3.555348061 1.885563062 4.941642423
138 1.691821236 3.556775613 1.885941572 4.886582645
139 1.746238611 3.891820298 1.972769702 5.363543151
140 1.679155631 3.642966397 1.908655652 4.754882459
141 1.94348069 4.156536582 2.038758589 6.277601677
142 1.549402462 3.250374492 1.8028795 3.342508385
143 1.856975574 4.232023463 2.057188242 6.413458957
144 2.529503815 5.684310793 2.38417927 11.22830537
145 2.035545742 4.643428898 2.154861689 7.244227516
146 2.467132416 5.697093487 2.386858497 11.50287513
147 2.298324686 4.870031331 2.206814748 9.286469586
148 1.937388065 4.34601078 2.0847088 7.322972679
149 1.956955486 4.536730733 2.129960266 7.739019572
150 2.036823984 4.518958489 2.125784206 8.594154233
151 1.972996546 4.529692045 2.128307319 7.967481199
152 1.58746864 3.283839256 1.812136655 3.314186005
153 1.521311054 3.464922216 1.861430153 3.681603045
154 2.44446969 5.445011746 2.333454895 10.3609124
155 2.294121109 4.731979033 2.17531125 9.105210941
156 3.126345733 6.927557906 2.632025438 15.6772624
157 1.867746396 4.253056253 2.06229393 6.32459191
158 1.839082858 4.029806041 2.00743768 5.382980154
159 2.127330896 4.844974178 2.201130205 7.863266724
160 2.404523583 5.236441963 2.288327329 10.04902409
161 2.262955985 4.845642719 2.201282063 9.034969801
162 2.253418218 4.727387819 2.174255693 9.130463484
163 2.302083991 5.167955549 2.273313781 10.06411762
164 2.192165626 4.835011259 2.198865903 9.262695602
165 1.672685332 3.734489965 1.93248285 4.565493369
166 1.568460311 3.539508997 1.881358285 3.52282487
167 1.609819887 3.523868735 1.877197042 3.920784511
168 1.616583967 3.587676949 1.894116403 4.394572604
169 1.643301653 3.654700957 1.911727218 3.912023005
170 1.621923158 3.581532841 1.892493815 3.891820298
171 2.090637708 4.527208645 2.127723818 8.536995819
172 2.109497906 4.585222548 2.141313277 8.203668045
173 2.03091153 4.429625613 2.104667578 7.785783239
174 2.09487893 4.582924577 2.140776629 8.204589814
175 2.040382454 4.335786342 2.08225511 6.632541816
176 2.312894869 5.342334252 2.311349011 9.798127037
177 1.430087263 3.148453361 1.774388165 2.939161922
178 2.293711966 4.871098263 2.20705647 9.392661929
179 2.391075023 4.894101478 2.212261621 9.375295332
180 2.517077345 5.718436483 2.391325257 11.47221284
181 1.989024673 4.154969184 2.038374152 6.872128101
182 2.02016078 4.294014757 2.072200463 7.403304815
183 1.797360845 4.076689627 2.019081382 5.90560705
184 1.705239225 3.931825633 1.982883162 5.697965589
185 1.471533812 3.312439025 1.820010721 3.529590596
186 1.438083095 3.346917175 1.829458164 3.533978493
187 1.619261465 3.559624618 1.886696748 4.109233175
188 1.609819887 3.6558396 1.912025 4.166355098
189 2.346796539 5.146965796 2.26869253 9.872567414
190 1.784208279 3.519720884 1.876091918 4.879539029
191 1.832126365 3.811539467 1.952316436 5.259368616
192 1.677986168 3.452840615 1.858182073 3.885884348
193 1.966109701 4.163870625 2.04055645 6.526348436
194 1.701367309 3.828641396 1.956691441 4.605170186
195 1.931534652 4.279440046 2.06868075 6.927802974
196 1.36183801 3.102342009 1.761346646 2.645465326
197 2.432819556 5.883322388 2.425556099 10.46486408
198 2.078341803 4.564943223 2.136572775 7.650468513
199 1.432099112 3.171155089 1.780773733 2.931193752
200 2.174427741 4.839451482 2.199875333 8.482392615
201 2.16404302 4.710430697 2.170352666 8.620246046
202 1.738643812 3.737669618 1.933305361 5.834810737
203 2.303817478 5.000921602 2.236274044 9.718344619
204 1.741189967 3.731819205 1.931791708 5.090062428
205 1.794671893 3.904293207 1.975928442 5.247024072
206 1.757635562 3.857777991 1.964122703 5.006560336
207 1.676226207 3.66137978 1.913473224 4.566637236
208 1.77911412 3.86388263 1.965676125 5.669260041
209 2.059914227 4.564348191 2.136433521 7.695152987
210 1.32424147 3.104586678 1.761983734 2.182674796
211 1.604334732 3.751518852 1.936883799 4.85787254
212 1.662497734 3.79739748 1.948691222 5.073109185
213 1.44885795 3.04690056 1.745537327 2.907447359
214 2.487551021 5.598973005 2.366214911 10.97673998
215 2.438166592 5.528436532 2.351262753 10.75773968
216 1.892477044 4.164647686 2.040746845 7.15334893
217 1.520482581 3.272335343 1.80895974 3.424588334
218 2.488969385 5.681996883 2.383693957 10.74868607
219 2.215837149 4.53044664 2.128484588 7.620705087
220 2.442786243 5.526780079 2.350910479 10.69919132
221 2.570602875 5.907702431 2.430576563 11.59161344
222 2.608344119 6.053264948 2.460338381 12.33182385
223 2.524368131 5.738731256 2.395564914 11.20612853
224 1.539964086 3.38269391 1.839210132 3.571221411
225 1.541550744 3.476614021 1.864568052 3.523119986
226 2.111209474 4.695924549 2.167008202 8.126284621
227 1.910391851 4.139955073 2.034687955 6.467590025
228 2.801971864 6.015864434 2.452725919 13.0280527
229 2.616209119 5.780126041 2.404189269 11.53329656
230 2.570130461 5.673975975 2.38201091 10.97701107
231 2.545595117 5.629669374 2.372692431 11.14107887
232 2.618299253 5.800606659 2.408444863 11.97035031
233 2.443348195 5.385412073 2.320649063 10.85417971
234 2.385152788 5.279188197 2.297648406 10.67131308
235 2.512400994 5.685007319 2.384325338 11.58593194
236 2.39352554 5.12693575 2.26427378 10.4590302
237 1.823796962 3.992680908 1.998169389 6.109247583
238 1.768267491 3.745968421 1.935450444 5.260096154
239 2.376820756 5.302583255 2.302733865 10.4487146
240 2.042402374 4.477336814 2.115971837 7.810068783
241 2.159700495 4.673996377 2.161942732 8.189916149
242 1.948229832 4.378018613 2.092371528 6.932447892
243 1.330510703 3.059880093 1.749251295 2.083184528
244 1.464097665 3.342685111 1.828301154 3.072693315
245 1.446917352 3.196630216 1.787912251 2.829087196
246 2.082252099 4.60990894 2.14706985 8.075582637
247 1.933494729 4.136126096 2.033746812 7.003065459
248 1.840298976 3.949126093 1.987240824 7.056175284
249 1.649584193 3.645188765 1.909237745 4.51129897
250 1.778648064 3.883623531 1.97069113 5.09681299
251 2.526339825 5.903056741 2.429620699 11.66907415
252 2.512244141 5.734958092 2.394777253 10.93748043
253 1.947599667 4.356708827 2.08727306 6.514712691
254 2.181687439 4.946274535 2.224022153 8.799405331
255 2.109497906 4.510859507 2.123878411 8.132101136
256 1.831713667 4.188138442 2.046494183 6.109247583
257 1.5517319 3.446807893 1.856558077 3.765840495
258 2.47549747 5.727881894 2.393299374 10.78967984
259 1.96580772 4.156693187 2.038796995 6.229496711
260 1.978602442 4.21508618 2.053067505 7.258412151
261 2.064000486 4.339901708 2.083243075 7.670717659
262 2.117775721 4.510639702 2.123826665 7.731676304
263 2.221912965 4.838923916 2.199755422 8.877208949
264 1.940925986 4.266896327 2.065646709 6.450865289
265 2.040382454 4.579852378 2.140058966 7.857666456
266 2.173143952 4.666735542 2.160262841 8.561717125
267 2.240859653 4.901564199 2.21394765 8.808442394
268 1.888874933 4.080921542 2.02012909 6.163314804
269 1.845529749 4.082609306 2.020546784 6.885284696
270 2.238519604 4.984229093 2.23253871 8.987910316
271 2.393206767 5.29338648 2.300736073 10.70491521
272 2.702044102 5.884714177 2.425842983 12.3883942
273 2.219296721 4.854631045 2.203322728 9.263449766
274 1.96161829 4.090838423 2.022582118 6.993932975
275 2.00561407 4.171305603 2.042377439 7.324270223
276 2.467836387 5.578051269 2.361789844 10.8016414
277 1.390119244 3.100092289 1.760707894 2.379546134
278 1.365322726 3.044760505 1.744924212 2.401525041
279 1.598782218 3.516726026 1.875293584 4.234975692
280 1.94538671 4.131961426 2.032722663 6.199494461
281 2.172592522 4.89858579 2.213274902 8.804952261
282 1.908668224 4.102312732 2.025416681 6.374172668
283 1.944434766 4.112266337 2.027872367 6.54672802
284 1.58387445 3.505557397 1.872313381 3.941581808
285 1.743721514 3.832670536 1.95772075 5.11349268
286 1.592453126 3.549329989 1.883966557 4.871143315
287 1.283414418 2.79971739 1.673235605 1.54329811
288 1.320439849 2.90690106 1.704963654 2.070653036
289 1.194572818 2.708716646 1.645817926 1.184789985
290 1.231175294 2.681021529 1.637382524 1.115141591
291 1.365322726 3.074795481 1.753509476 2.254444718
292 1.408422528 3.000719815 1.732258588 2.422144328
293 2.184734225 4.886582645 2.210561613 8.803574418
294 2.030652566 4.649187071 2.156197364 7.901007052
295 1.890679763 4.02356438 2.005882444 6.212726329
296 1.855414729 4.027135813 2.006772486 5.858647185
297 1.819146836 3.737669618 1.933305361 5.340274716
298 1.51380043 3.337192052 1.826798306 3.514823642
299 1.923936518 4.162158962 2.040136996 6.485993092
300 2.54480266 5.875913394 2.42402834 11.44989333
301 2.015083881 4.471638793 2.114624977 7.725330038
302 1.902054478 4.30514559 2.074884476 7.141300544
303 1.932189012 4.149463861 2.037023284 7.112433389
304 1.357151358 2.977059008 1.725415605 2.054123734
305 2.172040349 4.677490848 2.162750759 7.584422406
306 2.12856108 4.80073697 2.191058413 8.165269798
307 1.597383378 3.38269391 1.839210132 3.948162052
308 1.571436916 3.451890496 1.857926397 3.970291914
309 1.669116161 3.728100167 1.930828881 4.514479321
310 1.792870023 3.818920387 1.95420582 5.395716273
311 2.701422654 6.042632834 2.45817673 12.51412704
312 2.724885462 6.056784013 2.461053436 12.63817968
313 2.649668658 5.96870756 2.44309385 12.34583459
314 1.328012928 3.19047635 1.786190457 2.231089091
315 2.290836238 4.827072968 2.197060074 8.67484801
316 2.375600157 5.495650681 2.344280419 10.05803872
317 1.625886455 3.693369359 1.92181408 5.110178924
318 2.329332455 5.313205979 2.305039258 9.613803477
319 1.515480102 3.456316681 1.859117178 3.185525845
320 1.472454994 3.284663565 1.812364082 3.025291076
321 1.506165026 3.349904087 1.83027432 3.054001182
322 1.473374347 3.306520335 1.81838399 3.25617161
323 1.527068855 3.325036021 1.82346813 3.36729583
324 2.110354575 4.662495253 2.159281189 8.165363632
325 1.523787537 3.514526067 1.874706928 3.062923523
326 2.023599447 4.094344562 2.02344868 6.938769333
327 2.753898938 5.917548864 2.432601255 12.66032792
328 2.617941755 5.678362097 2.382931408 11.46939146
329 2.119034653 4.483002552 2.117310216 8.240121298
330 2.066147705 4.476199805 2.115703147 7.14397299
331 2.101925481 4.630837933 2.151938181 7.659327016
332 2.508777239 5.407171771 2.325332615 11.8987611
333 2.005568244 4.463030419 2.112588559 7.397665697
334 1.726738648 3.759687344 1.938991321 4.430816799
335 1.774901671 3.812975852 1.952684268 4.937562683
336 1.648959883 3.423610976 1.85030024 3.988984047
337 1.777714463 3.797733859 1.948777529 5.403847868
338 1.704136403 3.63758616 1.9072457 5.272486607
339 1.844729114 3.968445871 1.992095849 6.429622699
340 1.768267491 3.797733859 1.948777529 5.523339153
341 2.159320704 4.744410253 2.178166718 8.301035184
342 2.109497906 4.580877493 2.140298459 7.726636028
343 2.521315024 5.573617308 2.360850971 11.60597801
344 2.576758408 5.79269513 2.406801847 11.8427421
345 2.669803365 5.872117789 2.423245301 12.427118
346 2.441001399 5.430134791 2.330264962 11.48863277
347 2.117775721 4.750395438 2.17954019 8.556413905
348 2.023599447 4.553734634 2.133948133 7.34601021
349 2.394268344 5.066826574 2.250961255 9.954988325
350 2.106053393 4.696472344 2.167134593 7.892825526
351 2.100394247 4.736330019 2.176311103 7.72870183
352 2.160269524 4.922204729 2.21860423 8.255482913
353 2.188997276 4.774912961 2.185157422 9.409191231
354 1.874905013 3.86388263 1.965676125 6.003887067
355 2.061842158 4.182126476 2.045024811 6.745236349
356 1.418864374 3.119939077 1.766334928 2.7631695
data<-read.csv(data.csv,header=T)
fit.all<-lm(a~x+y+z,data=data)
b<-data.frame(data,predict(fit.all,interval="prediction"))
ggplot(data,aes(x=x+y+z,y=a))+
geom_point(size=3,shape=1,col="black")+
geom_smooth(method="lm")+
geom_line(aes(y=lwr), color = "red", linetype = "dashed")+
geom_line(aes(y=upr), color = "red", linetype = "dashed")+
theme_classic()