[R] differing number of rows when running nls regression

Gabor Grothendieck ggrothend|eck @end|ng |rom gm@||@com
Sat Sep 20 15:17:38 CEST 2025


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 using 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 using 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



More information about the R-help mailing list