Сравнение линейной регрессии с лог-линейной регрессией с помощью R - PullRequest
2 голосов
/ 11 июля 2020

У меня есть модель в R, где я уменьшил цену Honda civi c на ее пробеге:

civic <- read.csv("civic.csv")
c <- civic

plot (c$Mileage, c$Price,
      xlab = "Mileage",
      ylab = "Price")

regrPM1 <- lm(Price~Mileage, data = c)

abline (regrPM1, col="red",lwd=3)

Это дает мне следующее:

Участок 1

Пока все хорошо. Теперь у меня есть другая модель:

regrPM2 <- lm(log(c$Price)~c$Mileage)

И я хочу добавить соответствующую линию регрессии в Plot1 сверху. Когда я использую команду abline:

abline(regrPM2, col="green", lwd=3)

, получается следующий график:

Участок 2

Теперь это нельзя использовать для сравнения двух моделей. Я ищу способ сравнить их без использования логарифмической шкалы. Я думаю, что мог бы использовать кривую спроса, чтобы получить лучшие результаты, но это еще не сработало.

Благодарен за любую помощь!

Ответы [ 2 ]

2 голосов
/ 12 июля 2020

Трудно продемонстрировать, что здесь не так, без данных, поэтому я попытаюсь создать что-то, примерно похожее на ваше:

set.seed(69)

m <- rgamma(5000, 2, 2) * 30000
p <- 3e4 * log((rnorm(5e3, 1e4, 1e3) + m)/(m + rnorm(5e3, 5e3, 5e2)) + rgamma(5000, 2, 2)/8)

c <- data.frame(Mileage = m, Price = p)

plot (c$Mileage, c$Price,
      xlab = "Mileage",
      ylab = "Price")

enter image description here

This is close enough for demonstration purposes.

Now we can add the linear regression line using your code:

regrPM1 

enter image description here

Now, if we regress the log of the price on the mileage, we will get the same flat green line as you did if we just plot the result using abline:

regrPM2 

enter image description here

That's because we are plotting the log of the price on the (non-logged) plot. We want to take the anti-log of the result of our regression and plot that.

Note that it's better to use the data argument in our lm call, so let's do:

regrPM3 

Now instead of trying to plot this as a straight line, let's take the anti-log of its predictions at fixed intervals and plot them:

lines(seq(0, 2e5, 1e3), 
      exp(predict(regrPM3, newdata = list(Mileage = seq(0, 2e5, 1e3)))),
      col = "blue", lty = 2, lwd = 4)

введите описание изображения здесь

Итак, синяя пунктирная линия - это то, как выглядит регрессия журнала.

2 голосов
/ 12 июля 2020

Это не прямая линия в исходном масштабе. Вы можете сделать что-то вроде следующего, чтобы показать нелинейный прогноз в исходном масштабе.

DF <- data.frame(Mileage=seq(1, 150000, 1))
pred <- predict(regrPM2, newdata=DF)
lines(DF$Mileage, exp(pred))

Это должно быть выполнено после вы создадите начальный график с plot()

...