0

I am trying to plot both linear and inverse fit lines on a scatterplot. I have attempted a good number of things without success.

I have attempted using ggplot with smooth function specification, but the inverse line still totally looks like y=0. I've tried adding ablines, no luck. I have not had success using lm(y=1/x).

  • 1
    What do you mean by *inverse fit lines*? Note that `lm(y=1/x)` is not valid R, it could be `lm(y ~ I(1/x))` but I still wouldn't understand the point. – Rui Barradas Jul 28 '19 at 21:13
  • By inverse fit, do you mean something like an exponential decay model, e.g. https://stats.stackexchange.com/questions/136269/model-for-exponential-decay-with-lots-of-zeros? – Weihuang Wong Jul 28 '19 at 21:36
  • like `ggplot(mtcars, aes(hp, mpg)) + geom_point() + geom_smooth(method = 'glm', method.args = list(family = quasi('inverse')))`? [A reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would help a lot. – alistaire Jul 28 '19 at 21:54

1 Answers1

1

What I understand from the inverse fitted line is ;

y = a + bx

data <- data.frame("y"=mtcars$disp,"x"=mtcars$wt)

fit <-  lm(data$y ~  data$x)
fit_inverse <-  lm(data$y ~  I(1/data$x))

However, since the x axises are different (x and 1/x) , to put them on the same graph you must use different x axises. Otherwise, you need to plot them separately.

plot(data$x,data$y,col = "blue",bty="l",pch=20,ylab = "",xlab="")
lines(data$x,fitted(fit) ,type="l",lty = 29,col="blue") 
par(new = TRUE)
plot(1/data$x,data$y, xaxt = "n", yaxt = "n",col = "red", lty = 
2,bty="l",pch=10,ylab = "",xlab="")
lines(1/data$x,fitted(fit_inverse) ,type="l",lty = "29",col="red") 
axis(side = 3)
legend("top", c("Fitted", "Inverse Fitted"),col = c("blue", "red"), lty 
= c(29, 2))

enter image description here

maydin
  • 3,715
  • 3
  • 10
  • 27