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.
Related
I have the following code
glmer(Success ~ Trial*Treatment + 1|ID + Origin + Shelter, family = binomial, data = rama)
It gives me a significant effect of the interaction between Trial and Treatment on the response variable success.
I would like to plot this interaction.
I tried to use
plot(Success ~ Trial*Treatment, data = rama)
But it doesn't plot the interaction, rather makes two different plots, one Success vs Trial and another Success vs Treatment.
Ideally, I would like to merge these two graphs, a bit as what I would get if it was an LME instead of a GLME and I plotted (if success was a continuous variable) and I'd use the following code
boxplot(Success ~ Trial*Treatment, data = rama)
I would be super happy if anyone has an idea.
Thanks a lot!
I need to extract the baseline hazards from a general survival model (GSM) that I've constructed using the rstpm2-package (a conversion of the stpm2 module in stata).
using the data in the rstpm2-package let's use this as an example:
library(rstpm2)
gsm <- stpm2(Surv(rectime,censrec==1)~hormon, data=brcancer, df=3)
sum.gsm <- summary(gsm)
So I've noticed that the summary has an element named bhazard:
sum.gsm#args$bhazard
However it seems to be filled with zeroes and holds one value per patient. As far as I understand the baseline hazard should consist of one hazard for every time-point in the data.
Does anyone have any experience that could be of assistance
You can use the plot and lines methods to plot survival and a number of other predictions. For example:
plot(gsm, newdata=data.frame(hormon=0))
Plot
Note that you need to specify the newdata argument. For more general plots, you can get predictions on a time grid with full covariates with standard errors using:
out <- predict(gsm,newdata=data.frame(hormon=0:1), grid=TRUE,
full=TRUE, se.fit=TRUE)
Then you could use ggplot2 or lattice to plot the intervals. For example,
library(ggplot2)
ggplot(out, aes(x=rectime, y=Estimate, ymin=lower, ymax=upper,
fill=factor(hormon))) +
geom_ribbon(alpha=0.6) + geom_line()
Plot
Edit: to predict survival at specific times, you can include the times in the newdata argument:
predict(gsm, newdata=data.frame(hormon=0,rectime=1:365))
(Note that survival is defined in terms of log(time), hence I have excluded rectime=0.)
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
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))
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)
)