I have create a multivariate linear regression model in R called modelP. I want to plot this in ggplot, but can't work this out. The model is:
modelP <- lm(House_Price ~ FactorA + FactorB + FactorC, data=df)
I have plotted individual linear regression lines for each factor, but I want to create a graph of the model combining all factors. I would like to do something like this:
ggplot(df)+
geom_smooth(aes(y = House_Price, x = FactorA + FactorB + FactorC))
Or
ggplot(modelP$model)+
geom_smooth(method="lm")
But neither approach seems like it will work. I would really appreciate any help.
Related
I have a generalized linear model (family - gamma) with interaction, and need to plot it specifically in ggplot2 (on a response scale).
The model was constructed with following code:
fit1mult = glm(SIZE_OOCYTE ~ TREATMENT * CASTE,
family = Gamma(link = "log"), data=data1)
I made a plot using the lattice package, and it looks like this:
How can I make a similar one in ggplot? I know that the geom_smooth has an option glm, but don't know how to apply it.
Thanks in advance for suggestions!
UPD. Solution:
library(ggeffects)
mydf1 <- ggpredict(fit1mult, terms = c("CASTE", "TREATMENT"))
plot(mydf1)
Result:
new plot
As dependent variables, I have a data frame with 0s and 1s (using certain product or not). As independent variables, I have a set of data frames with categorical variables (living in brick house, etc.). I plot logistic regression using ggplot:
g <- ggplot(decision, aes(x=decision_point, y=use)) + geom_point(alpha=.1, size=2, col="red") +
geom_smooth(method = "glm",
method.args = list(family = "binomial"),
aes(x = as.numeric(decision_point)),
se = F)
What happens is that it plots a straight line. It seems the categorical variables are turned into numeric (as I wrote) and it just goes through it.
But if I don't use as.numeric, no line shows at all.
What can I do? The line should be a curve. So if independent variables were incremental numeric values, like 0-100, then plotting a curve would be easy. But they are categorical variables, like "Brick House" "Hut", "Others". Hence the problem. Thank you in advance.
I created an interaction term with iv*sex and imputed the data with mice. Then used the imputed data to run a logistic regression model (glm):
model <- with(data=imp, glm(dv~control+iv+sex+iv*sex, family="binomial"))
The following are the abbreviations of the variable names:
dependent variable=dv, independent variable=iv, moderator=sex, interaction term= iv*sex
There is significant interaction for iv*sex and I would like to plot a graph for the interaction but I couldn't find how to. It will be greatly appreciated if any solutions is offered. Thanks!
I've just run into the same issue, and with effects package I solved it.
e <- effects::effect("iv*sex", model)
e <- as.data.frame(e)
ggplot2::ggplot(e, ggplot2::aes(iv, fit, color=sex, group = sex)) +
ggplot2::geom_point() +
ggplot2::geom_line() +
"fit" is your dependent variable, in your case "dv".
I am trying to find a way to get a scatterplot in R of actual values vs. regressed values. Example:
fit = lm(y ~ a + x + z)
I get the results y ~ 2*a + 3*x - 7*z + 4
Now how do I make a scatterplot plotting y against 2*a + 3*x - 7*z + 4? As well as creating a trendline.
(And, by the way, I tried the plot() function. It didn't seem to have what I need)
Look at plot(fit), or the help for lm, which you can access using ?lm.
From your question, it sounds like you want to plot your actual values against the fitted values. There is a plot method for lm which does this out of the box.
You could always build it yourself, say in ggplot2, by accessing the fitted values. Check out your object using str(fit) for all of the data that captured during the regression.
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)
)