Luigi Marongiu
2025-Sep-20 06:02 UTC
[R] differing number of rows when running nls regression
I have some data (y=Response, x=Dose) that is distributed roughly as a curve. I am trying to fit a non-linear regression model on the data using negative logistic function (even if the data show a rise in the final part of the curve that the logistic won't accommodate for). I used a rough graphical approach to guess the starting values then the package `nlstools` for a better overview of the data. However, I get the error "differing number of rows". If I run `nls` directly, I get the error" "Error in 1 + exp : non-numeric argument to binary operator" Why is that happening? How can I set the model? Is there a better curve function to fit the data? Thank you ``` df = data.frame(Response = c(890.72, 895.46, 895.63, 894.72, 895.49, 893.59, 892.53, 895.06, 897.21, 889.27, 876.05, 857.96, 862.02, 858.36, 890.94, 890.8, 887.22, 888.91, 890.83, 889.92, 891.76, 890.32, 886.35, 878.11, 866.57, 859.04, 863.64, 880.16, 884.15, 879.57, 878.89, 882.27, 881.59, 880.98, 881.45, 876.19, 868.32, 859.16, 850.53, 853.21, 859.34, 859.73, 861.19), Dose = c(0.0000000015, 0.000000003, 0.000000006, 0.000000012, 0.000000024, 0.000000048, 0.000000095, 0.00000018, 0.00000038, 0.00000078, 0.0000015, 0.000013, 0.000025, 0.00005, 0.0000000015, 0.000000003, 0.000000006, 0.000000012, 0.000000024, 0.000000048, 0.000000095, 0.00000018, 0.00000038, 0.00000078, 0.0000015, 0.000025, 0.00005, 0.0000000015, 0.000000003, 0.000000006, 0.000000012, 0.000000024, 0.000000048, 0.000000095, 0.00000018, 0.00000038, 0.00000078, 0.0000015, 0.000003, 0.000006, 0.000013, 0.000025, 0.00005) ) plot(Response~log10(Dose), df) abline(a=500, b=-60) abline(v=log10(0.3e-6)) A = 0.3e-6 # plateau B = -60 # slope library(nlstools) # NL regression form = as.formula(Response ~ ( (exp(a+b*Dose)) / (1+exp-(a+b*Dose)) ) ) preview(form, data = x_25_2024, start=list(a=A, b=B)) nls(Response ~ ( (exp(a+b*Dose)) / (1+exp-(a+b*Dose)) ), data=df, start=list(a=A, b=B)) ```
Eric Berger
2025-Sep-20 10:25 UTC
[R] differing number of rows when running nls regression
I tried running your code. I did the following i. I omitted the preview() statement because x_25_2024 was not provided ii. I omitted the start=list(a=A,b=B) from the nls() statement. iii. I ran the nls command using the form that you defined> foo <- nls(form, data=df)It ran with no errors.> plot(foo)worked. FYI my sessionInfo()> sessionInfo()R version 4.5.1 (2025-06-13) Platform: x86_64-pc-linux-gnu Running under: Ubuntu 22.04.5 LTS Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0 locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C time zone: Asia/Jerusalem tzcode source: system (glibc) attached base packages: [1] stats graphics grDevices datasets utils methods base other attached packages: [1] nlstools_2.1-0 nlme_3.1-166 loaded via a namespace (and not attached): [1] compiler_4.5.1 tools_4.5.1 bspm_0.5.7 grid_4.5.1 lattice_0.22-6 HTH, Eric On Sat, Sep 20, 2025 at 9:02?AM Luigi Marongiu <marongiu.luigi at gmail.com> wrote:> I have some data (y=Response, x=Dose) that is distributed roughly as a > curve. I am trying to fit a non-linear regression model on the data > using negative logistic function (even if the data show a rise in the > final part of the curve that the logistic won't accommodate for). > I used a rough graphical approach to guess the starting values then > the package `nlstools` for a better overview of the data. However, I > get the error "differing number of rows". > If I run `nls` directly, I get the error" "Error in 1 + exp : > non-numeric argument to binary operator" > Why is that happening? > How can I set the model? > Is there a better curve function to fit the data? > Thank you > > ``` > df = data.frame(Response = c(890.72, 895.46, 895.63, > 894.72, 895.49, 893.59, > 892.53, 895.06, 897.21, 889.27, 876.05, > 857.96, 862.02, 858.36, > 890.94, 890.8, 887.22, 888.91, 890.83, > 889.92, 891.76, 890.32, > 886.35, 878.11, 866.57, 859.04, 863.64, > 880.16, 884.15, 879.57, > 878.89, 882.27, 881.59, 880.98, 881.45, > 876.19, 868.32, 859.16, > 850.53, 853.21, 859.34, 859.73, 861.19), > Dose = c(0.0000000015, 0.000000003, > 0.000000006, 0.000000012, > 0.000000024, 0.000000048, 0.000000095, > 0.00000018, > 0.00000038, 0.00000078, 0.0000015, > 0.000013, 0.000025, > 0.00005, 0.0000000015, 0.000000003, > 0.000000006, > 0.000000012, 0.000000024, 0.000000048, > 0.000000095, > 0.00000018, 0.00000038, 0.00000078, > 0.0000015, 0.000025, > 0.00005, 0.0000000015, 0.000000003, > 0.000000006, > 0.000000012, 0.000000024, 0.000000048, > 0.000000095, > 0.00000018, 0.00000038, 0.00000078, > 0.0000015, 0.000003, > 0.000006, 0.000013, 0.000025, 0.00005) > ) > plot(Response~log10(Dose), df) > abline(a=500, b=-60) > abline(v=log10(0.3e-6)) > A = 0.3e-6 # plateau > B = -60 # slope > library(nlstools) # NL regression > form = as.formula(Response ~ ( (exp(a+b*Dose)) / (1+exp-(a+b*Dose)) ) ) > preview(form, data = x_25_2024, start=list(a=A, b=B)) > nls(Response ~ ( (exp(a+b*Dose)) / (1+exp-(a+b*Dose)) ), > data=df, start=list(a=A, b=B)) > ``` > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Gabor Grothendieck
2025-Sep-20 13:17 UTC
[R] differing number of rows when running nls regression
Be sure to use log10(Dose) because of the order of magnitude variation in Dose. Also R has a self starting SSlogis function so putting those together. df.2 <- transform(df, log10.Dose = log10(Dose)) |> sort_by(~list(log10.Dose)) fm <- nls(Response ~ SSlogis(log10.Dose, Asym, xmid, scal), df.2) plot(Response ~ log10.Dose, df.2) lines(fitted(fm) ~ log10.Dose, df.2, col = "red") You could also try:(continuing from above): fm2 <- lm(Response ~ poly(log10.Dose, 4), df.2) lines(fitted(fm2) ~ log10.Dose, df.2, col = "blue") On Sat, Sep 20, 2025 at 2:02?AM Luigi Marongiu <marongiu.luigi at gmail.com> wrote:> > I have some data (y=Response, x=Dose) that is distributed roughly as a > curve. I am trying to fit a non-linear regression model on the data > using negative logistic function (even if the data show a rise in the > final part of the curve that the logistic won't accommodate for). > I used a rough graphical approach to guess the starting values then > the package `nlstools` for a better overview of the data. However, I > get the error "differing number of rows". > If I run `nls` directly, I get the error" "Error in 1 + exp : > non-numeric argument to binary operator" > Why is that happening? > How can I set the model? > Is there a better curve function to fit the data? > Thank you > > ``` > df = data.frame(Response = c(890.72, 895.46, 895.63, > 894.72, 895.49, 893.59, > 892.53, 895.06, 897.21, 889.27, 876.05, > 857.96, 862.02, 858.36, > 890.94, 890.8, 887.22, 888.91, 890.83, > 889.92, 891.76, 890.32, > 886.35, 878.11, 866.57, 859.04, 863.64, > 880.16, 884.15, 879.57, > 878.89, 882.27, 881.59, 880.98, 881.45, > 876.19, 868.32, 859.16, > 850.53, 853.21, 859.34, 859.73, 861.19), > Dose = c(0.0000000015, 0.000000003, > 0.000000006, 0.000000012, > 0.000000024, 0.000000048, 0.000000095, > 0.00000018, > 0.00000038, 0.00000078, 0.0000015, > 0.000013, 0.000025, > 0.00005, 0.0000000015, 0.000000003, > 0.000000006, > 0.000000012, 0.000000024, 0.000000048, > 0.000000095, > 0.00000018, 0.00000038, 0.00000078, > 0.0000015, 0.000025, > 0.00005, 0.0000000015, 0.000000003, > 0.000000006, > 0.000000012, 0.000000024, 0.000000048, > 0.000000095, > 0.00000018, 0.00000038, 0.00000078, > 0.0000015, 0.000003, > 0.000006, 0.000013, 0.000025, 0.00005) > ) > plot(Response~log10(Dose), df) > abline(a=500, b=-60) > abline(v=log10(0.3e-6)) > A = 0.3e-6 # plateau > B = -60 # slope > library(nlstools) # NL regression > form = as.formula(Response ~ ( (exp(a+b*Dose)) / (1+exp-(a+b*Dose)) ) ) > preview(form, data = x_25_2024, start=list(a=A, b=B)) > nls(Response ~ ( (exp(a+b*Dose)) / (1+exp-(a+b*Dose)) ), > data=df, start=list(a=A, b=B)) > ``` > > ______________________________________________ > 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.-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com