Integrating a multiple regression equation (including covariates) into ggplot2 graph - r

I am currently generating simple linear regression plots in ggplot2 with the following code (toy example)
library(ggplot2)
Data<-data.frame(Age=c(40,41,42,43,44,45,46,47,48,49),
Height=c(185,184,183,182,181,186,187,188,189,190),
Sex=c("Male","Male","Male","Male","Male","Female","Female","Female","Female","Female"),
Weight=c(84,83,82,81,80,85,86,87,88,89),
BMI=c(20,21,22,23,24,25,26,27,28,29))
points<-c("#FF9999","turquoise")
lines<-c("dark red","blue")
linetypes<-c(1,1)
x<-ggplot(Data,aes(x=Age,y=Weight))+
geom_point(aes(fill=factor(Sex)),size=4,alpha=1,shape=21,color="transparent")+
geom_smooth(aes(color=Sex,linetype=Sex),method="lm",formula=y~x,lwd=1,se=F)+
scale_fill_manual(values=points,labels=c("Female","Male"))+
scale_colour_manual(values=lines)+
scale_linetype_manual(values=linetypes)+
theme_classic(base_size=15)+
theme(legend.position="top",legend.direction="horizontal")+
labs(fill="Points",colour="Lines",x='Age',y='Weight')+
guides(fill=guide_legend(override.aes=list(shape=c(16,16),alpha=1,size=4,color=c("#FF9999","turquoise"))))+
guides(colour=guide_legend(override.aes=list(linetype=c(1,1),alpha=1,size=1,color=c("dark red","blue"))))+
guides(linetype="none")
x
I am stuck as how to add one-or-more covariates, in order to transform the plots from simple to multiple linear regression, e.g., from
Weight~Age to Weight~Age+Height+BMI
I am sure that it must have something to do with the method="lm" and formula=y~x commands, but I am not clear on exactly what changes need to be made.
Any help would be very much appreciated. Thank you.

Related

Is there an R function for creating an interaction plot of a panelAR model?

In order to strengthen the interpretation of an interaction term I would like to create an interaction plot.
Starting Point: I am analyzing a panel data frame with which I fitted a feasible generalized least squares model by using the panelAR function. It includes an interaction term of two continuous variables.
What I want to do: To create an interaction plot, e.g. following the style of “plot_model” from the package sjPlot (see Three-Way-Interactions: link).
Problem: I could neither find any package which supports the type of my model nor a different way to get a plot.
Question: Is there any workaround which can be used for obtaining an interaction plot or even a package which supports a panelAR model?
Since I am quite new to R I would appreciate every kind of help. Thank you very much

ggadjustedcurves, fun="cumhaz" does not work

In the 'survminer' package I have been able to construct adjusted curves using cox model but this only shows me the surival function. When I try to input "events" or "cumhaz" into fun= option this only gives me the same survival function. I found this link
https://github.com/kassambara/survminer/issues/287
Wondering if anyone have any suggestions?
I took the advice of Chung30916 in the comment chain and used the following code
plotdata2<-plotdata%>%
mutate(cumhaz=1-surv)
to make a cumulative incidence curve but, forgive me for my inexperience, how do I proceed? Just plot the graph in ggplot2 using the strata (2 groups in my case) and the x will be the time whereas y will be the cumhaz?
Thanks

Trouble with abline

I'm having trouble plotting 2 abline()s on a graph of the log10 Brain mass and log10 body mass. I'm following someone else's script but it just doesn't work for me. This is what I have:
This is the graph produced:
Why are the lines off there like that? Are the values I'm getting for the intercept and slope incorrect, or am I using the wrong ones? I've done other examples of this and its worked OK, but I've always ended up using the first model, never the second one so I'm not sure if I'm using the right values.
If you want to represent a linear regression of the log of the body mass compared to the log of the brain mass, the code is:
model <- lm(log10(brain)~log10(body))
then
abline(model$coefficients[2], model$coefficients[1])
When you don't know which parameter to enter in a function, use the help of that function. For abline(), the first parameter is the slope and the second one is the intercept.
Currently, your model use log10(brain), log10(body) and class.
If you want to assess the quality of your model, look at the residuals.
plot(model)
You can also just use the result of your lm like this:
model <- lm(log10(brain)~log10(body))
plot(log10(brain)~log10(body))
abline(model,col=2)

Function to plot model with one variable varying and others constant

It's simple, but I can't remember how this procedure is called, hence I was not able to find the function to do so. I want to explore the effects and gradients of a simple lm() model by plotting the response of one variable at a time, the others being kept constant.
Can anybody tell me which function to use to do so? I seem to remember it's a function generating several plots, or something like this. It could be something akin to sensitivity analysis... Sorry for the beginner question.
Thank you in advance!
The car package has a lot of utilities for analyzing regression models. This sounds like a component+residual plot (or partial residuals plot).
library(car) # for avPlots(...)
fit <- lm(mpg~wt+hp+disp, mtcars)
crPlots(fit)
As noted in the comments, termplot(...) does basically the same thing.

Order lattice panel by regression intercept

Example dataset here
Let us build a simple lattice plot from this data for linear regression, with separate panels for each Subject
xyplot(Measurement~HOL|Subject,groups=Treatment,data=Data,
type=c('p','r'),auto.key=T,aspect="xy")
The issue is, I would like to visually inspect if the slopes-and-intercepts are correlated. Thus, I would like to order the panels by linear-regression intercept as opposed to by Subject (this was done in Douglas Bates' book "lme4: Mixed-effects modeling with R" Figure 3.1, but I cannot find example code). I know I can change the order of panels by hand by adding
index.cond=list(c(1,2,3, etc))
But this is extraordinarily inefficient, especially since I would like to do this for multiple response variables.
Does anyone have an automated way to do this? I am also open to attempting this in ggplot2 if it has any built in functions, but as I understand, there is no way to easily change the aspect to a 45degree such as the
aspect="xy"
does in Lattice.
Thank you in advance for any thoughts
If you want to order by regression intercept, it's best to run the regression. For example, with your data we can do
cf<-sapply(Data$Subject, function(x)
coef(lm(Measurement~HOL, data=subset(Data, Subject==x))))
which will give a slope/intercept for each person, we can then create a new factor of Subjects ordered by the intercept with
Sx<-reorder(Data$Subject, cf[1,])
and then use that variable as the grouping variable in the plot
xyplot(Measurement~HOL|Sx,groups=Treatment,data=Data,
type=c('p','r'),auto.key=T,aspect="xy")
And in ggplot you can fix the ratio of x/y with +coord_fixed(ratio=1)

Resources