Creating interaction effect plot, ggplot or other - r

I have found an interaction effect between the predictors age and education level in a multiple regression model assessing the effects of various predictors on alcohol consumption. I wish to graph this interaction effect using ggplot, but an alternative will do.
I have attempted to do it this way:
p <- ggplot(DataFrame, aes(ED,AGE, label=interaction-effect))
p <- p + geom_point(colour= "red")+geom_text(size=3) # colour = colr does not work
p
This is continuously spouting errors. I cannot seem to find a way to simply make this plot and would greatly appreciate help.

A straight-forward google search yields fairly good results:
http://www.r-bloggers.com/visual-interpretation-of-interaction-terms-in-linear-models-with-ggplot-rstats/
Interaction Plot in ggplot2
https://stats.stackexchange.com/questions/9477/how-to-draw-an-interaction-plot-with-confidence-intervals
http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/#error-bars-for-within-subjects-variables

Related

How to convert the y-axis of a plot from log(y) to y

I'm an R newbie. I want to estimate a regression of log(CONSUMPTION) on INCOME and then make a plot of CONSUMPTION and INCOME.
I can run the following regression and plot the results.
results <- lm(I(log(CONSUMPTION)) ~ INCOME, data=dataset)
effect_plot(results, pred=INCOME)
If I do this, I get log(CONSUMPTION) on the vertical axis rather than CONSUMPTION.
How can I get a plot with CONSUMPTION on the vertical axis?
Another way to ask the question is how do I convert the y-axis of a plot from log(y) to y? While my question is for the function effect_plot(), I would be happy with any plot function.
Thanks for any help you can give me.
Thank you for the responses. I was able to figure out a workaround using Poisson regression:
results1 <- glm(CONSUMPTION ~ INCOME+WEALTH, family=poisson, data=Consumption )
effect_plot(results1,pred=INCOME,data=Consumption)
This allows me to identify the effect of one variable (INCOME) even when the regression has more than one explanatory variable (INCOME+WEALTH), and plots the estimated effect with CONSUMPTION on the vertical axis rather than ln(CONSUMPTION), with INCOME on the horizontal axis.
The associated estimates are virtually identical to what I would get from the log-linear regression:
results2 <- lm(I(log(CONSUMPTION)) ~ INCOME+WEALTH, data=Consumption )
I appreciate you for taking the time to help me with my problem.

Plotting Marginal effects of interactions with continuous and dummy

I have a doubt about my homework on how to plot multiple interactions and I would be very grateful if anyone could help. I know this is a popular topic, but I have not seen questions addressing my specific problem. I hope is not a repetition
I have a model with 3 explanatory variables and two interactions. Two variables are continuous and one is a dummy. I would like to plot into a single image the marginal effect of the three interactions.
the model is something like this
y = a+ b_1c1 + b_2c2 + b_3d + b_4c1c2 + b_5c1d
In sum, I have one continuous variable (c1), that is interacted both with the other continuous variable (c2) and the dummy variable (d).
The code below uses data from the MASS package to allow a reproducible example.
library(interplot)
library(MASS) # to take the dataset from
data<- cars93
library(interplot) # for plotting interations
and I create my model
model<- lm(Price ~ Horsepower*EngineSize # c1*c2 interaction
+ Horsepower*Man.trans.avail, #c1*d interaction
data = data)
I would like to plot the estimated effects of the Horsepower on price as a function of the EngineSize for Man.trans.avail=0 (No),
and plot that same conditional effect of Horsepower on price as a function of the EngineSize for Man.trans.avail=1 (Yes). Possibly putting them into the same plot.
Using the interplot function I am able to see the conditional Horsepower on price as a function of the EngineSize but without controlling for Man.trans.avail
interplot(m=model, var1 = "Horsepower", var2 = "EngineSize", hist = TRUE)
The result plots the marginal effect line of Horsepower and EngineSize, however, is not able to plot two lines for different values of Man.trans.avail and I was wondering whether you had some idea on how to plot marginal effects in similar circumstances.
I thank you a lot in advance for your replies

plot interaction terms in a mixed-model

I would like to visualize the effect of a significant interaction term in the following mixed effects model.
c1 <- lmer(log.weight ~ time + I(time^2) + temp + precip + time:precip + time:temp + (1|indiv), data = noctrl)
This model includes fixed effects of 'time' (simple and quadratic term), 'temperature', 'precipitation' and two interactions on the logarithmized response 'weight'. All terms are significant and the models assumptions of normality and homogeneity are met.
I’ve been using the 'effects' package to produce interaction plots to show the effect of the interactions. When I try to show the interaction of time and temperature (time:temp) with the following code I’m not sure whether the resulting plot correctly shows this interaction.
ef2 <- effect(term="time:temp", c1, multiline=TRUE)
y <- as.data.frame(ef2)
ggplot(y , aes(time, fit, color=temp)) + geom_point() + geom_errorbar(aes(ymin=fit-se, ymax=fit+se), width=0.4)
I need help understanding the resulting plot please. How come despite this interaction term is highly significant, the SEs overlap at each value of x?
Am I using the effects package correctly? Or is it because I need to include the quadratic term of time I(time^2) in the interaction terms as well?
Thank you very much for the clarification.

R: visreg for gam with interactive variables

Hi I am currently estimating happiness and age using the gam model.
My command for the regression is
fit <- gam(happy ~ s(age) + s(age, by=nochild) ,data=happy)
and I would like to visualise the estimated effect of the interactive variable "s(age):nochild" using visreg. I am struggling to do so as only the estimation for age and nochild has shown up when using
visreg(fit)
I want the graph to look like this
Y axis : s(age):nochild
X axis : age
However, I am not sure how to do this with visreg or is there any other commands that can help to do this?
Thank you so much in advance
I got the code that I need now so I think I might share this.
plot(fit, select=2, main="No Children")

how to plot estimates through model in R

I'm trying to use R to do some modelling, I've started to use BodyWeight library, since I've seen some examples online. Just to understand and get used to the commands.
I've come to my final model, with estimates and I was wondering how to plot these estimates, but I haven't seen anything online..
Is there a way to plot the values of the estimates with a line, and dots for the values of each observation?
Where can I find information about how to do this, do I have to extract the values myself or it is possible to say plot the estimates of these model?
I'm only starting with R. Any help is welcome.
Thank you
There is no function that just plots the output of a model, since there are usually many different possible ways of plotting the output.
Take a look at the predict function for whatever model type you are using (for example, linear regressions using lm have a predict.lm function).
Then choose a plotting system (you will likely want different panels for different levels of diet, so use either ggplot2 or lattice). Then see if you can describe more clearly in words how you want the plot to look. Then update your question if you get stuck.
Now we've identified which dataset you are using, here's a possible plot:
#Run your model
model <- lme(weight ~ Time + Diet, BodyWeight, ~ 1 | Rat)
summary(model)
#Predict the values
#predict.lme is a pain because you have to specify which rat
#you are interested in, but we don't want that
#manually predicting things instead
times <- seq.int(0, 65, 0.1)
mcf <- model$coefficients$fixed
predicted <-
mcf["(Intercept)"] +
rep.int(mcf["Time"] * times, nlevels(BodyWeight$Diet)) +
rep(c(0, mcf["Diet2"], mcf["Diet3"]), each = length(times))
prediction_data <- data.frame(
weight = predicted,
Time = rep.int(times, nlevels(BodyWeight$Diet)),
Diet = rep(levels(BodyWeight$Diet), each = length(times))
)
#Draw the plot (using ggplot2)
(p <- ggplot(BodyWeight, aes(Time, weight, colour = Diet)) +
geom_point() +
geom_line(data = prediction_data)
)

Resources