Luigi Marongiu
2025-Apr-11 15:53 UTC
[R] How to predict intersection value between regression and line in R?
I am trying to predict the intersection value between a curve and a line. I can fit a logistic model to the data by converting the data to the range 0-1. How can I determine the intersection with a line? Also, is there a way to do the same without converting the data? Here is an example: ``` val = c(120.64, 66.14, 34.87, 27.11, 8.87, -5.8, 4.52, -7.16, -17.39, -14.29, -20.26, -14.99, -21.05, -20.64, -8.03, -21.56, -1.28, 15.01, 75.26, 191.76, 455.09, 985.96, 1825.59, 2908.08, 3993.18, 5059.94, 6071.93, 6986.32, 7796.01, 8502.25, 9111.46, 9638.01, 10077.19, 10452.02, 10751.81, 11017.49, 11240.37, 11427.47, 11570.07, 11684.96, 11781.77, 11863.35, 11927.44, 11980.81, 12021.88, 12058.35, 12100.63, 12133.57, 12148.89, 12137.09) df = data.frame(Cycles = 1:35, Values = val[1:cyc]) M = max(df$Values) df$Norm = df$Values/M df$Norm[df$Norm<0] = 0 b_model = glm(Norm ~ Cycles, data=df, family=binomial) x = 0.15 plot(Norm ~ Cycles, df, main="Normalized view", xlab=expression(bold("Amplification cycle")), ylab=expression(bold("Fluorescence (normalized)")), type="l", lwd=3, col="blue") lines(b_model$fitted.values ~ df$Cycles, col="red", lwd=2, lty=2) abline(h=x, col="green", lwd=2) ``` Thank you
Duncan Murdoch
2025-Apr-11 18:05 UTC
[R] How to predict intersection value between regression and line in R?
Your example doesn't run because it uses a variable `cyc` which you didn't include. But in general, I think you are mixing up two separate problems here. The first problem is creating a function of x (which is Cycles in your dataset). You used the predictions from a glm() to do this. That's a reasonable way to create a function with a range of 0 to 1, but you want a function with a different range. Dividing by the max is one way to get it, but there are others. I'd say a better approach would be to think about the problem from a subject matter point of view and determine a class of functions that match your needs. After you've done that, you might use glm() to find the particular version of that function (i.e. the unknown parameters), or maybe some other method. The second problem is finding where that function intersects a line. In your sample code, you were trying to intersect a horizontal line. If that's always the case, then it's a problem of inverting the function from the first problem. Just write out y = f(x) and solve for x = f^-1(y). If the formula isn't convenient, you can approximate the answer using the R function uniroot(). Duncan Murdoch On 2025-04-11 11:53 a.m., Luigi Marongiu wrote:> I am trying to predict the intersection value between a curve and a line. > I can fit a logistic model to the data by converting the data to the > range 0-1. How can I determine the intersection with a line? > Also, is there a way to do the same without converting the data? > Here is an example: > > ``` > val = c(120.64, 66.14, 34.87, 27.11, 8.87, -5.8, > 4.52, -7.16, -17.39, > -14.29, -20.26, -14.99, -21.05, -20.64, -8.03, > -21.56, -1.28, 15.01, > 75.26, 191.76, 455.09, 985.96, 1825.59, 2908.08, > 3993.18, 5059.94, > 6071.93, 6986.32, 7796.01, 8502.25, 9111.46, > 9638.01, 10077.19, > 10452.02, 10751.81, 11017.49, 11240.37, 11427.47, > 11570.07, 11684.96, > 11781.77, 11863.35, 11927.44, 11980.81, 12021.88, > 12058.35, 12100.63, > 12133.57, 12148.89, 12137.09) > df = data.frame(Cycles = 1:35, Values = val[1:cyc]) > M = max(df$Values) > df$Norm = df$Values/M > df$Norm[df$Norm<0] = 0 > b_model = glm(Norm ~ Cycles, data=df, family=binomial) > x = 0.15 > plot(Norm ~ Cycles, df, main="Normalized view", > xlab=expression(bold("Amplification cycle")), > ylab=expression(bold("Fluorescence (normalized)")), > type="l", lwd=3, col="blue") > lines(b_model$fitted.values ~ df$Cycles, col="red", lwd=2, lty=2) > abline(h=x, col="green", lwd=2) > ``` > Thank you > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.