Plotting mean and standard error of mean from linear regression

Viewed 60

I've run a multiple linear regression where pred_acc is the dependent continuous variable and emotion_pred and emotion_target are two dummy coded independent variables with 0 and 1. Furthermore I am interested in the interaction between the two independent variables.

model <- lm(predic_acc ~ emotion_pred * emotion_target, data = data_almost_final) 
summary(model)

    Residuals:
     Min       1Q   Median       3Q      Max 
-0.66049 -0.19522  0.01235  0.19213  0.67284 

Coefficients:
                            Estimate Std. Error t value Pr(>|t|)    
(Intercept)                  0.97222    0.06737  14.432  < 2e-16 ***
emotion_pred                 0.45988    0.09527   4.827 8.19e-06 ***
emotion_target               0.24383    0.09527   2.559 0.012719 *  
emotion_pred:emotion_target -0.47840    0.13474  -3.551 0.000703 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2858 on 68 degrees of freedom
  (1224 Beobachtungen als fehlend gelöscht)
Multiple R-squared:  0.2555,    Adjusted R-squared:  0.2227 
F-statistic: 7.781 on 3 and 68 DF,  p-value: 0.0001536

In case some context is needed: I did a survey where couples had to predict their partners preferences. The predictor individual was either in emotion state 0 or 1 (emotion_pred) and the target individual was either in emotion state 0 or 1 (emotion_target). Accordingly, there are four combinations.

Now I want to plot the regression with the means of each combination of the independent variables (0,1; 1,0; 1,1; 0,0) and add an error bar with the standard error of the means. I have literally no idea at all how to do this. Anyone can help me with this?

Here's an extraction from my data:

     pred_acc  emotion_pred  emotion_target
1  1.0000000              1               0
2  1.2222222              0               1
3  0.7777778              0               0
4  1.1111111              1               1
5  1.3888889              1               1

Sketch of how I want it to look like

1 Answers

Using emmip from the emmeans library:

model <- lm(data=d2, pred_acc ~ emotion_pred*emotion_target)

emmip(model, emotion_pred ~ emotion_target, CIs = TRUE, style = "factor")

enter image description here

If you want more control over the image or just to get the values you can use the emmeans function directly:

> emmeans(model , ~ emotion_pred * emotion_target )

 emotion_pred emotion_target emmean    SE df lower.CL upper.CL
            0              0  0.778 0.196  1   -1.718     3.27
            1              0  1.000 0.196  1   -1.496     3.50
            0              1  1.222 0.196  1   -1.274     3.72
            1              1  1.250 0.139  1   -0.515     3.01

Then you can use ggplot on this dataframe to make whatever graph you like.

Related