2023-10-14

How to force `stat_poly_line()` to use a specific non-zero y-intercept?

Using stat_poly_line() from package 'ggpmisc', one can fit a polynomial to data by default using lm() as method. You can force the fit through zero with either: formula = y ~ x + 0 or formula = y ~ x - 1. I cannot force it through a specific non-zero y-intercept for my linear model. In this case, I need to force it through 5.05.

Note: I recognize linear models are rarely statistically useful when the y-intercept is forced, but in my case I believe it is fine.

Here is my data:

mydata <- structure(list(y = c(20.2, 29.74, 22.37, 24.51, 
37.2, 31.43, 43.05, 54.36, 65.44, 67.28, 46.02), x = c(0.422014140000002, 
1.09152966, 1.3195521, 3.54231348, 2.79431778, 3.40756002, 5.58845772, 
7.10762298, 9.70041246, 11.7199653, 15.89668266)), row.names = c(NA, 
-11L), class = c("tbl_df", "tbl", "data.frame"))

And here is a simplified version of my plot:

myplot <- ggplot(mydata, aes(x = x, y = y)) +
  stat_poly_line(se = FALSE, 
                 linetype = "dashed", 
                 na.rm = TRUE, 
                 formula = y ~ x + 0) +
  stat_poly_eq(use_label(c("eq", "R2", "adj.R2")), 
               na.rm = TRUE, 
               formula = y ~ x + 0) +
  geom_point(size = 2.5) 

The x-value of the variable is 0 but I tried using 5.05 in that place to represent a y-intercept at 5.05 for the linear model (the x + 0 comes from the packages guide for how to put parabola intercepts at 0). This approach does not work, nor does using it on the y side of the formula either.

I could use another package relatively quickly, but I feel like there is a simple solution I can implement here.

Any help?



No comments:

Post a Comment