Questions tagged [nls]

`nls` is an R routine to fit nonlinear regression models of the form $y = f(x B) + e$

nls is the R procedure to fit a nonlinear regression model characterized by $y = f(xB) + e$, where $f$ is some non-linear function relating the response to the predictors and $e$ is a stochastic "error" term. The nls function is part of the stats package. More information can be found in John Fox's appendix on Nonlinear Regression and Nonlinear Least Squares.

113 questions
29
votes
3 answers

Why is nls() giving me "singular gradient matrix at initial parameter estimates" errors?

I have some basic data on emission reductions and cost per car: q24 <- read.table(text = "reductions cost.per.car 50 45 55 55 60 62 65 70 70 80 75 90 80 100 85 200 90 375 95 600 ",header = TRUE,…
Amanda
  • 421
  • 1
  • 4
  • 7
25
votes
2 answers

Singular gradient error in nls with correct starting values

I'm trying to fit a line+exponential curve to some data. As a start, I tried to do this on some artificial data. The function is: $$y=a+b\cdot r^{(x-m)}+c\cdot x$$ It is effectively an exponential curve with a linear section, as well as an…
steiny
  • 375
  • 1
  • 3
  • 7
17
votes
2 answers

Change point analysis using R's nls()

I'm trying to implement a "change point" analysis, or a multiphase regression using nls() in R. Here's some fake data I've made. The formula I want to use to fit the data is: $y = \beta_0 + \beta_1x + \beta_2\max(0,x-\delta)$ What this is supposed…
JoFrhwld
  • 2,247
  • 3
  • 20
  • 22
16
votes
1 answer

How to minimize residual sum of squares of an exponential fit?

I have the following data and would like to fit a negative exponential growth model to it: Days <- c( 1,5,12,16,22,27,36,43) Emissions <- c( 936.76, 1458.68, 1787.23, 1840.04, 1928.97, 1963.63, 1965.37, 1985.71) plot(Days, Emissions) fit <-…
Strohmi
  • 815
  • 1
  • 10
  • 13
15
votes
2 answers

How to test the effect of a grouping variable with a non-linear model?

I have a question regarding the use of a grouping variable in a non-linear model. Since the nls() function does not allow for factor variables, I have been struggling to figure out if one can test the effect of a factor on the model fit. I have…
Marc in the box
  • 3,532
  • 3
  • 33
  • 47
14
votes
4 answers

Getting the right starting values for an nls model in R

I'm trying to fit a simple power law model to a data set that is as follows: mydf: rev weeks 17906.4 1 5303.72 2 2700.58 3 1696.77 4 947.53 5 362.03 6 The goal being to pass the power line through and use it to predict rev vlaues for future…
NeonBlueHair
  • 389
  • 1
  • 2
  • 9
10
votes
2 answers

Fitting exponential decay with negative y values

I am trying to fit an exponential decay function to y-values that become negative at high x-values, but am unable to configure my nls function correctly. Aim I am interested in the slope of the decay function ($\lambda$ according to some sources).…
Mikko
  • 1,172
  • 2
  • 19
  • 31
9
votes
1 answer

Choose best model between logit, probit and nls

I'm analyzing a certain dataset, and I need to understand how to choose the best model that fits my data. I'm using R. An example of data I have is the following: corr <- c(0, 0, 10, 50, 70, 100, 100, 100, 90, 100, 100) These numbers correspond to…
Tommaso
  • 563
  • 2
  • 5
  • 17
9
votes
1 answer

nonlinear least squares versus maximum likelihood in R, nls() or nlm()?

I am estimating the model $$E(Y|X) = Pr(Y=1|X) = \alpha_0 + (1 - \alpha_0 - \alpha_1)\phi(X'\beta),$$ where $\alpha_0$ and $\alpha_1$ are parameters, $\beta$ is a $p$-length vector of parameters, $X$ is a $p \times n$ matrix of data, $Y$ the…
gfgm
  • 390
  • 3
  • 14
7
votes
1 answer

Fitting a "sigmoid" function: Why is my fit so bad?

I tried to fit a curve to the black points using the following code. Why is the fit so bad? Do I need to fit another type of function? fit <- nls(grad ~ theta1/(1 + exp(-(theta2 + theta3*x1))), start=list(theta1 = 4, theta2 = 0.09,…
user32289
  • 71
  • 1
  • 2
7
votes
0 answers

Numerical properties of the logistic growth model for non-linear regression

I am using the nls procedure in R to fit a logistic growth model. In their SSlogis function, José Pinheiro and Douglas Bates chose the formulation Asym / (1 + exp((xmid-input) / scal)) for their model. As I am fairly inexperienced with the…
Rob Hall
  • 1,182
  • 1
  • 7
  • 21
7
votes
1 answer

How to fit piecewise constant (or step-function) model and compare to logistic model in R

I have x, y data where x is position (along a transect) and y is a continuous variable (e.g.…
user2414840
  • 263
  • 1
  • 3
  • 4
6
votes
1 answer

Non-linear modelling with several variables including a categorical variable

I am trying to model some data regarding a predator prey interaction experiment (n=26). Predation rate is my response variable and I have 4 explanatory variables: predator density (1,2,3,4 5), predator size, prey density (5,10,15,20,25,30) and prey…
Robbie
  • 407
  • 4
  • 15
6
votes
2 answers

Interpreting and troubleshooting nls in R with quadratic plateau model

I am trying to run a quadratic plateau model on some proportion data where values are bound between 0 and 100. I would like some help troubleshooting some errors I have encountered, and correctly interpreting results as well as understanding the…
6
votes
1 answer

syntax for nls model with breakpoint

So the saga continues... So I am trying to fit the model $$Runoff = \begin{cases} \beta_0 + \beta_1Pcp & \text{if $(Ant + Pcp) < Thold$;}\\ \beta_2 + \beta_3Pcp & \text{if $(Ant + Pcp) \geq Thold$;} \end{cases}$$ Where $Pcp$ and $Ant$ are observed…
Iceberg Slim
  • 261
  • 2
  • 11
1
2 3 4 5 6 7 8