plot allEffects confidence intervals into one panel - r

I am trying to plot in only one panel the lines and confidence intervals of my linear mixed model, but I get 2 panels. How do I change this?
f is a factor with 2 levels,
c is a continuous variable
If I use multiline=TRUE, I loose the confidence intervals, which I need.
mymodel = lmer(depvar ~ f*c+ (0 + f+c|pp) + (1|stim), data = datafile2)
allEffects(mymodel)# call the particular model you want to see all the effect of
plot(allEffects(mymodel))

Related

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

A question of plotting binary logistic regression in R

#First, we'll create a fake dataset of 20 individuals of different body sizes:
bodysize=rnorm(20,30,2) # generates 20 values, with mean of 30 & s.d.=2
bodysize=sort(bodysize) # sorts these values in ascending order.
survive=c(0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1) # assign 'survival' to these 20 individuals non-randomly... most mortality occurs at smaller body size
dat=as.data.frame(cbind(bodysize,survive)) # saves dataframe with two columns:
body size & survival
dat # just shows you what your dataset looks like. It will look something like this:
plot(bodysize,survive,xlab="Body size",ylab="Probability of survival") # plot with body size on x-axis and survival (0 or 1) on y-axis
g=glm(survive~bodysize,family=binomial,dat) # run a logistic regression model (in this case, generalized linear model with logit link). see ?glm
curve(predict(g,data.frame(bodysize=x),type="resp"),add=TRUE) # draws a curve based on prediction from logistic regression model
points(bodysize,fitted(g),pch=20) # optional: you could skip this draws an
invisible set of points of body size survival based on a 'fit' to glm model.
pch= changes type of dots.
I want to know where the 'x'(data.frame(bodysize=x)) come from?

What are the differences between directly plotting the fit function and plotting the predicted values(they have same shape but different ranges)?

I am trying to learn gam() in R for a logistic regression using spline on a predictor. The two methods of plotting in my code gives the same shape but different ranges of response in the logit scale, seems like an intercept is missing in one. Both are supposed to be correct but, why the differences in range?
library(ISLR)
attach(Wage)
library(gam)
gam.lr = gam(I(wage >250) ~ s(age), family = binomial(link = "logit"), data = Wage)
agelims = range(age)
age.grid = seq(from = agelims[1], to = agelims[2])
pred=predict(gam.lr, newdata = list(age = age.grid), type = "link")
par(mfrow = c(2,1))
plot(gam.lr)
plot(age.grid, pred)
I expected that both of the methods would give the exact same plot. plot(gam.lr) plots the additive effects of each component and since here there's only one so it is supposed to give the predicted logit function. The predict method is also giving me estimates in the link scale. But the actual outputs are on different ranges. The minimum value of the first method is -4 while that of the second is less than -7.
The first plot is of the estimated smooth function s(age) only. Smooths are subject to identifiability constraints as in the basis expansion used to parametrise the smooth, there is a function or combination of functions that are entirely confounded with the intercept. As such, you can't fit the smooth and an intercept in the same model as you could subtract some value from the intercept and add it back to the smooth and you have the same fit but different coefficients. As you can add and subtract an infinity of values you have an infinite supply of models, which isn't helpful.
Hence identifiability constraints are applied to the basis expansions, and the one that is most useful is to ensure that the smooth sums to zero over the range of the covariate. This involves centering the smooth at 0, with the intercept then representing the overall mean of the response.
So, the first plot is of the smooth, subject to this sum to zero constraint, so it straddles 0. The intercept in this model is:
> coef(gam.lr)[1]
(Intercept)
-4.7175
If you add this to values in this plot, you get the values in the second plot, which is the application of the full model to the data you supplied, intercept + f(age).
This is all also happening on the link scale, the log odds scale, hence all the negative values.

Plot data from Poisson GLM in R

I have some count data in two categories, and have carried out a poisson GLM to look for differences in the two categories:
mod <- glm(count ~ category, family = "poisson")
For a poster, I want a simple plot of the results - probably a bar chart with error bars. Is there an easy way to get the plot data from mod? Do the error bars correspond to the model Std. Error? For what it's worth, I plan to use ggplot2 to make the figure.
Also, am I right in thinking that, for a Poisson GLM, the model units are the log of 'real' units - so if the Estimate for categoryB is, say, 0.4 then this corresponds to observations in Category B having, on average, exp(0.4) = 1.5 higher count than Category A?

How Would You Plot CIs with an ANCOVA model

I'm working with a data set that involves a response variable (mass), a covariate (length), and a categorical independent variable (location) of a certain bird species. This data set looks like this:
In this case there are three locations (Orlando, Tampa, and another Floridian City) with ten observations each). I decided to run an ANCOVA using the lm function.
m1<- lm(mass.d ~ length.d + location)
I was able to plot this model with ggplot:
predm1<- predict(m1)
ggplot(YOUR_DATA, aes(length.d, mass.d, color = pop)) + geom_point() +
geom_line(aes(y= predm1))
Now I would like to add my 95 percent confidence intervals.
I have the confidence intervals but I have very little idea on how to input them into my ggplot graph because I essentially have three different slopes for 3 different locations. I've been playing around with geom_ribbon but have not been able to find an answer.

Resources