Question: Thank you in advance! does nlsLM
not work with diff
? I am simply trying to do
g*(p[i]-p[i-1])/(x[i]-x[i-1])
and use nlsLM
to find the value of fitting parameter g that can fit y
I use diff
without nlsLM
as follows,
`g*diff(df$p)/diff(df$x)`
and it works just fine:
`[1] 0.4 3.0 3.0`
However, when I use it with nlsLM
, it does not work and I get the following:
`Error in qr(.swts * attr(rhs, "gradient")) :
dims [product 3] do not match the length of object [4]
In addition: Warning messages:
1: In lhs - rhs :
longer object length is not a multiple of shorter object length
2: In lhs - rhs :
longer object length is not a multiple of shorter object length
3: In lhs - rhs :
longer object length is not a multiple of shorter object length
4: In lhs - rhs :
longer object length is not a multiple of shorter object length
5: In lhs - rhs :
longer object length is not a multiple of shorter object length
6: In lhs - rhs :
longer object length is not a multiple of shorter object length
7: In lhs - rhs :
longer object length is not a multiple of shorter object length
8: In .swts * attr(rhs, "gradient") :
longer object length is not a multiple of shorter object length`
Code:
# Packages:
library(tidyverse)
library(minpack.lm)
# undoing tidyverse's masking effects( Courtesy of user Kat)
filter <- dyplr::filter
lag <- dplyr::lag
#df
df<-data.frame(x=c(9,14,15,17),p=c(11,13,16,22),y=c(16,19,25,35))
#object
g<-5
#nlsLM run: finding fitting parameter g's value
summary(nlsLM(formula=y~g*diff(p)/diff(x),
data=df,trace=F,
start=list(g=5),control=nls.lm.control(maxiter=1000)))