I'm trying to create a plot of the interactive effect of two variables on an outcome variable in a linear regression model using plot_model
. I have an interaction term in my model (BLK70*FP59), but when I use type = "int"
I get "Error: No interaction term found in model."
I can get the plot I want using type = "pred"
, but I want to change the values of the moderator term (FP59 below) in the interaction. How can I do this? See below example:
library(sjPlot)
model1 <- lm(HR70 ~ . - NAME - STATE_NAME,
data = crimeExFilt)
model2 <- update(model1, ~ . - PO60 - PO70 -
HC60 - HC70 - POL60 - POL70 -
RD60 - RD70 - PS60 - PS70)
model3 <- update(model2, ~ . - GI69)
model4 <- update(model3, ~ . - GI59)
model5 <- update(model4, ~ . - MFIL59)
model6 <- update(model5, ~ . - FP69)
model7 <- update(model6, ~ . - FH60)
model8 <- update(model7, ~ . - DV60)
model9 <- update(model8, ~ . - UE60)
model10 <- update(model9, ~ . - DNL70)
model11 <- update(model10, ~ . - DNL60)
model12 <- update(model11, ~ . - BLK60)
model13 <- update(model12, ~ . - UE70)
model14 <- update(model13, ~ . + BLK70*FP59)
plot_model(model14, type = "pred", terms = c("BLK70", "FP59"))
In the plot_model
documentation I can see the mdrt.values
function, but that doesn't work with type = "pred"
.
Also, how can I add more lines for differing values of FP59?
Edit: summary data is below from dput(head(crimeExFilt, 20))
. I removed one variable (NAME) as its not relevant to the model and I was over the character count.
structure(list(STATE_NAME = structure(c(22L,
46L, 46L, 46L, 46L, 11L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 33L,
33L, 33L, 33L, 33L, 33L, 33L), .Label = c("Alabama", "Arizona",
"Arkansas", "California", "Colorado", "Connecticut", "Delaware",
"District of Columbia", "Florida", "Georgia", "Idaho", "Illinois",
"Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
"Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
"New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin",
"Wyoming"), class = "factor"), SOUTH = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
HR60 = c(0, 0, 1.8638634161, 2.6123301985, 0, 0, 7.9763898859,
1.0111734668, 11.529038766, 0, 0, 3.5740452832, 0, 0, 0,
0, 2.9459419649, 0, 0, 0), HR70 = c(0, 0, 1.9151584794, 1.2886431876,
0, 0, 5.5361789293, 1.6894745734, 9.2738569971, 5.7087400811,
0, 3.8406882513, 0, 0, 0, 0, 0, 0, 0, 8.1172125492), HC60 = c(0,
0, 0.3333333333, 0.6666666667, 0, 0, 1, 0.3333333333, 1.3333333333,
0, 0, 0.6666666667, 0, 0, 0, 0, 0.3333333333, 0, 0, 0), HC70 = c(0,
0, 0.3333333333, 0.3333333333, 0, 0, 1, 0.6666666667, 1,
0.3333333333, 0, 0.6666666667, 0, 0, 0, 0, 0, 0, 0, 0.6666666667
), PO60 = c(4304L, 3889L, 17884L, 25520L, 6914L, 5809L, 12537L,
32965L, 11565L, 7904L, 2624L, 18653L, 6458L, 5566L, 5886L,
4698L, 11315L, 10641L, 5624L, 10064L), PO70 = c(3987L, 3655L,
17405L, 25867L, 6025L, 5484L, 18063L, 39460L, 10783L, 5839L,
2359L, 17358L, 5779L, 4564L, 4739L, 3828L, 9496L, 11549L,
4645L, 8213L), RD60 = c(-0.17510549, -0.836868285, -0.537371617,
-0.820169785, -0.93617014, -0.850319046, -1.40425979, -1.136291549,
-0.314596351, -1.245690305, -0.972029373, -1.149784334, -0.591604056,
-0.173043012, -0.048135919, -0.516882533, -0.550614009, 0.5252124234,
-0.549590586, -0.005570979), RD70 = c(-0.1965356, -0.847855576,
-0.225283145, -0.391126083, -0.45145694, -0.630179478, -1.542621404,
-0.884559174, 0.2010717469, -0.636244755, -0.730313484, -0.769094521,
-0.92383632, -0.582953792, -0.7195375, -0.893953808, -0.325512315,
0.6192301296, 0.0670489427, -0.022077556), PS60 = c(-1.449946096,
-1.707205577, -0.568146453, -0.554938608, -1.106438634, -1.20839428,
-0.98204569, -0.346093185, -0.966681816, -1.116701907, -1.864842975,
-0.592590111, -1.224457678, -1.248144375, -1.153201929, -1.247070389,
-0.787183304, -0.625011616, -1.164168197, -0.842095066),
PS70 = c(-1.462559387, -1.697719914, -0.591883439, -0.552016041,
-1.181753555, -1.21941739, -0.697968638, -0.2317217, -1.002008852,
-1.313787543, -1.878456577, -0.648495216, -1.274861798, -1.360590663,
-1.283436317, -1.362946989, -0.907080663, -0.56253244, -1.274896982,
-0.979372569), UE60 = c(7.9, 8.2, 10.1, 7.5, 10.6, 10.8,
17.3, 13.8, 12.8, 5.5, 5.1, 6.3, 5.5, 5.1, 3.5, 4.1, 5.3,
14.4, 6, 5.1), UE70 = c(9, 15.4, 9, 10.5, 13.4, 12.1, 10.3,
9.8, 12.6, 3.6, 0.5, 4.3, 3, 4, 4.2, 6, 4.7, 11.9, 2.3, 3.3
), DV60 = c(1.858974359, 2.8632784538, 2.7114466184, 3.3720410898,
3.0089878859, 2.5904203324, 3.4371395617, 2.5964850305, 3.8611039129,
3.3008252063, 2.39912759, 2.4852844997, 1.7998244074, 1.1499760422,
1.8885741265, 0.8891523414, 1.1185682327, 1.6148689823, 0.8713480267,
0.8155230596), DV70 = c(2.6198083067, 3.6860068259, 2.9763779528,
4.0903860236, 4.0106951872, 3.4893992933, 3.7966739881, 3.2997316702,
3.5012809564, 4.0658276864, 3.1137724551, 2.514011209, 1.9626168224,
1.529219006, 1.3264129181, 1.4035087719, 1.8281036835, 1.6844469399,
1.4084507042, 0.8643617021), MA60 = c(28.8, 25.9, 29.6, 29.4,
31.2, 27.6, 24.8, 30.1, 22.8, 25.9, 24.8, 24, 29.2, 29.2,
29.7, 27.9, 28.3, 19.5, 29.5, 27), MA70 = c(30.5, 27.1, 31.8,
31.1, 33.8, 30.3, 25.1, 28, 22.1, 29.4, 26.9, 24.2, 31.3,
34.6, 33.4, 30.7, 32, 18.7, 33.2, 31.4), POL60 = c(8.3673001018,
8.2659073342, 9.7916617374, 10.147217737, 8.8413036205, 8.667163718,
9.4364395511, 10.403201672, 9.3557385747, 8.9751242394, 7.8724551501,
9.83376227, 8.7730749513, 8.6244319421, 8.6803319288, 8.4548921652,
9.3338845581, 9.2724697434, 8.6347984335, 9.2167199789),
POL70 = c(8.2907943474, 8.2038513722, 9.7645128002, 10.160723304,
8.7036727584, 8.6095900407, 9.8016209261, 10.58304278, 9.2857260989,
8.6723148283, 7.7659930794, 9.7618087742, 8.6619859363, 8.425954711,
8.463581422, 8.2500977526, 9.1586259363, 9.3543541321, 8.4435466512,
9.0134735437), DNL60 = c(1.1887546181, 0.5687860169, 1.9752447537,
1.5715669765, 1.5956485529, 1.5164622604, 1.2165748092, 1.8589771411,
1.3614435882, 1.3995395879, 0.6007514432, 1.8520289834, 1.3382270761,
1.4543123986, 1.6601412205, 1.6681752146, 1.9091227962, 2.4557338628,
1.6849419785, 1.8955314222), DNL70 = c(1.1122488636, 0.5067300549,
1.9480958166, 1.585072543, 1.4580176908, 1.4588885831, 1.5817561842,
2.0388182491, 1.2914311125, 1.0967301767, 0.4942893725, 1.7800754875,
1.2271380611, 1.2558351675, 1.4433907137, 1.463380802, 1.7338641745,
2.5376182515, 1.4936901962, 1.692284987), MFIL59 = c(8.2201339572,
8.5197898173, 8.403352375, 8.4799066066, 8.5083542427, 8.423102268,
8.6094076754, 8.5926716526, 8.5504345252, 8.703340753, 8.6755635274,
8.7339161749, 8.4228825119, 8.3390230057, 8.2456469009, 8.3219079682,
8.3556149958, 8.1092249531, 8.4474142968, 8.2779202582),
MFIL69 = c(8.7083092959, 9.0067543199, 8.8688354928, 8.969923492,
8.8952185838, 8.946505026, 9.1810145426, 9.055672892, 8.942983666,
9.0478214425, 9.0595174822, 9.0961633269, 9.0596337546, 8.8192218576,
8.810012048, 8.9685235554, 8.9061223309, 8.7859982081, 8.8597894947,
8.7677962556), FP59 = c(36.3, 22.5, 30.9, 25, 26.7, 27.2,
16.8, 20, 28.7, 12.7, 16, 14.7, 27.9, 35.9, 37.8, 31.1, 25.7,
45.2, 25.7, 35.2), FP69 = c(16.1, 11.6, 15.9, 12.9, 18.3,
11.1, 5.9, 10.1, 23.4, 10.9, 8.1, 11.1, 8.2, 14.6, 14.6,
10.7, 13.3, 28.9, 14.7, 14.5), BLK60 = c(0.4414498141, 0.0771406531,
0.0782822635, 0.0979623824, 0.0289268152, 0, 0.0239291697,
0.0819050508, 0.2075226978, 0.0126518219, 0, 0.1286656302,
0.0929080211, 0.3413582465, 0, 0, 0.0353513036, 0.0939761301,
0.0177809388, 0.029809221), BLK70 = c(0.4765487835, 2.2982216142,
0.091927607, 0.2822128581, 0.0165975104, 0.0547045952, 0.0221447157,
0.0760263558, 0.027821571, 0, 0, 0.4551215578, 0.0173040318,
1.0297984224, 0.0211014982, 0, 0.0315922494, 0.05195255,
0, 0), GI59 = c(0.2852352228, 0.2561578434, 0.2839986298,
0.2585395475, 0.243262959, 0.2619385918, 0.2114246328, 0.2419263375,
0.3168864307, 0.2399049248, 0.2863521291, 0.2596778551, 0.2699909989,
0.3029912281, 0.2890553776, 0.2665788382, 0.2647430473, 0.3438081357,
0.2705343299, 0.3234004078), GI69 = c(0.3723362149, 0.3606653751,
0.3940828825, 0.3712181838, 0.3656140744, 0.3503513633, 0.2955509596,
0.335356855, 0.3813660816, 0.3548185932, 0.3793765473, 0.3441587348,
0.3427664904, 0.331764502, 0.341186868, 0.3280738529, 0.3836338256,
0.3764732409, 0.417577773, 0.4092742788), FH60 = c(11.279620853,
10.053475936, 9.2584369449, 9.0399002494, 8.2439299831, 7.1129707113,
6.8727392305, 7.8634122881, 12.644595134, 9.4444444444, 9.2257001647,
9.3560429305, 10.403530895, 11.547344111, 13.005390836, 9.8464317977,
11.198501873, 14.565425024, 11.881918819, 12.213740458),
FH70 = c(5.4, 2.6, 5.6, 8.1, 4.1, 6.8, 3.7, 6.4, 11.4, 8,
5.6, 7.5, 6, 5.5, 2.5, 5.3, 6.8, 13.5, 8.4, 6.4)), row.names = c(NA,
20L), class = "data.frame")
I'm posting an answer from the creator of the sjPlot package below. It includes the solution:
For marginal effects plots (type = "pred" and type = "eff"), plot_model() simply wraps the ggeffects package.
Please see these vignettes for different options for which values can be chosen: https://strengejacke.github.io/ggeffects/articles/effectsatvalues.html
(and as introduction) https://strengejacke.github.io/ggeffects/articles/marginaleffects.html
or this vignette from sjPlot: https://strengejacke.github.io/sjPlot/articles/plot_interactions.html
In your case, use eg
plot_model(model14, type = "pred", terms = c("BLK70", "FP59 [meansd]")) plot_model(model14, type = "pred", terms = c("BLK70", "FP59 [quart]"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.