Plotting the results of a regression with a logged dependent variable

Hi, 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.

If I run the following regression and plot the results...

results <- lm(I(log(CONSUMPTION)) ~ INCOME, data=Consumption)
effect_plot(results, pred=INCOME)

...I get log(CONSUMPTION) on the vertical axis rather than CONSUMPTION.

How can I get a plot with CONSUMPTION on the vertical axis?

1 Like

Welcome in the Community!

I do not know the function effect_plot.
I assume it is from a package that I did not install.
What is the origin (package) of this function?

Thank you for responding to my question. effect_plot() is part of the jtools package. However, I would be happy to use any plot function.

Another way to ask my question is how do I convert the y-axis of a plot from log(y) to y?

if the lm has 'log(response) ~ target' formula, and you use the precid() formula to assign the result to a variable, you will get the models predicted log(response) values into your desired variable name.
you can then make a new variable which is the previous variable applied to the function exp()

for example

library(tidyverse)

iris_setosa <- iris %>% filter(Species=="setosa") %>% select(-starts_with("Se"))

(setosa_model <- lm(log(Petal.Length)~Petal.Width,data=iris_setosa))

iris_setosa$logpred <- predict(setosa_model,newdata = iris_setosa)
iris_setosa$pred <- exp(iris_setosa$logpred)

iris_setosa_long <- pivot_longer(data=iris_setosa,
                                 cols=c(pred,Petal.Length),
                                 names_to="dataname",
                                 values_to = "value")

ggplot(data=iris_setosa_long,
       mapping=aes(x=Petal.Width,
                   y=value,
                   color=dataname)) + geom_point(size=4,alpha=.7) +
  lims(x=c(0,1),y=c(1,2.5))

Thank you for this. My problem is I want to tease out the estimated, nonlinear effect associated with just one variable and then plot that effect.

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 y 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.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.