R 线性对数回归模型的ggplot?

R 线性对数回归模型的ggplot?,r,R,如何在R中绘制对数线性模型? 目前,我正在这样做,但不确定这是否是正确/有效的方法: data(food) model1 <- lm(food_exp~log(income), data = food) temp_var <- predict(model1, interval="confidence") new_df <- cbind(food, temp_var) head(new_df) ggplot(new_df, aes(x = income, y

如何在R中绘制对数线性模型? 目前,我正在这样做,但不确定这是否是正确/有效的方法:

data(food)
model1 <- lm(food_exp~log(income), data = food)
temp_var <- predict(model1, interval="confidence")
new_df <- cbind(food, temp_var)
head(new_df)
ggplot(new_df, aes(x = income, y = food_exp))+
  geom_point() +
  geom_smooth(aes(y=lwr), color = "red", linetype = "dashed")+
  geom_smooth(aes(y=upr), color = "red", linetype = "dashed")+
  geom_smooth(aes(y = fit), color = "blue")+
  theme_economist()
数据(食品)

model1如果不需要提取参数,只需要绘图,可以直接在ggplot2中绘图

一些用于打印的假数据:

library(tidyverse)

set.seed(454)
income <- VGAM::rpareto(n = 100, scale = 20, shape = 2)*1000
food_exp <- rnorm(100, income*.3+.1, 3)

food <- data.frame(income, food_exp)


这将适用于置信区间,但添加预测区间时,您需要像之前一样拟合模型,生成预测区间。

您可以使用geom_smooth并直接将公式放入。它应该产生与您的拟合相同的结果(您也可以通过绘制来检查)

ggplot(新的_-df,aes(x=萼片宽度,y=萼片长度))+
几何点()+
几何点(aes(y=fit),color=“red”)+#您的原始贴合度
几何平滑(方法=lm,公式=y~log(x))#ggplot拟合

但我不希望x轴是对数轴
ggplot(food, aes(x = log(income), y = food_exp))+
  geom_point()+
  geom_smooth(method = "lm")+
  theme_bw()+
  labs(
    title = "Log Linear Model Food Expense as a Function of Log(income)",
    x = "Log(Income)",
    y = "Food Expenses"
  )