nls

R nls: fitting a curve to data

别等时光非礼了梦想. 提交于 2019-12-11 00:02:06
问题 I'm having trouble finding the right curve to fit to my data. If someone more knowledgeable than me has an idea/solution for a better fitting curve I would be really grateful. Data: The aim is to predict x from y dat <- data.frame(x = c(15,25,50,100,150,200,300,400,500,700,850,1000,1500), y = c(43,45.16,47.41,53.74,59.66,65.19,76.4,86.12,92.97, 103.15,106.34,108.21,113) ) This is how far I've come: model <- nls(x ~ a * exp( (log(2) / b ) * y), data = dat, start = list(a = 1, b = 15 ), trace =

R script - NLS not working

不羁岁月 提交于 2019-12-10 19:36:56
问题 I have 5 (x,y) data points and I'm trying to find a best fit solution consisting of two lines which intersect at a point (x0,y0), and which follow these equations: y1 = (m1)(x1 - x0) + y0 y2 = (m2)(x2 - x0) + y0 Specifically, I require that the intersection must occur between x=2 and x=3. Have a look at the code: #Initialize x1, y1, x2, y2 x1 <- c(1,2) y1 <- c(10,10) x2 <- c(3,4,5) y2 <- c(20,30,40) g <- c(TRUE, TRUE, FALSE, FALSE, FALSE) q <- nls(c(y1, y2) ~ ifelse(g == TRUE, m1 * (x1 - x0)

how to use method=“nlsLM” (in packages minpack.lm) in geom_smooth

…衆ロ難τιáo~ 提交于 2019-12-10 15:41:28
问题 test <- data.frame(Exp = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6), t = c(0, 0.33, 0.67, 1, 1.33, 1.67, 2, 4, 6, 8, 10, 0, 33, 0.67, 1, 1.33, 1.67, 2, 4, 6, 8, 10, 0, 0.33, 0.67, 1, 1.33, 1.67, 2, 4, 6, 8, 10), fold = c(1, 0.957066345654286, 1.24139015724819, 1.62889151698633, 1.72008539595879, 1.82725412314402, 1.93164365299958, 1.9722929538061, 2.15842019312484, 1.9200507796933, 1.95804730344453, 1, 0.836176542548747, 1

Failing to do fitting with non linear fitting methods (nlsLM, nlxb and wrapnls)

Deadly 提交于 2019-12-09 23:45:41
问题 I have a nls fitting task that I wanted to do with R. My first attempt to do this here and as @Roland pointed out "The point is that complex models are difficult to fit. The more so, the less the data supports the model until it become impossible. You might be able to fit this, if you had extremely good starting values." I can agree with @Roland but if excel can do this fitting why not R cannot do? Basically this fitting can be done with Excel's GRG Nonlinear solver but the process is very

Do a nonlinear least square (nls) fit for a sinusoidal model

白昼怎懂夜的黑 提交于 2019-12-09 11:33:07
问题 I want to fit the following function to my data: f(x) = Offset+Amplitude sin(Frequency T+Phase), or according to Wikipedia: f(x) = C+alpha sin(omega T+phi) my data is stored in a file in two columns, and I import them by the following: data<-read.table("C:/PATH/data.txt", header = FALSE, sep = "\t") and convert them by the following: minV<-data[3] cV<-data[1] values<-as.numeric(unlist(minV)) T<-as.numeric(unlist(cV)) Thus I get two variable of type double, one called "T" (equivalent to time T

R: minpack.lm::nls.lm failed with good results

会有一股神秘感。 提交于 2019-12-08 19:14:21
问题 I use nls.lm from the minpack.lm package to fit a lot of non linear models. It often fails after 20 iterations because of a singular gradient matrix at initial parameter estimates. The problem is when I have a look at the iterations before failling ( trace = T ) I can see the results was ok. Reproductible example: Data: df <- structure(list(x1 = c(7L, 5L, 10L, 6L, 9L, 10L, 2L, 4L, 9L, 3L, 11L, 6L, 4L, 0L, 7L, 12L, 9L, 11L, 11L, 0L, 2L, 3L, 5L, 6L, 6L, 9L, 1L, 7L, 7L, 4L, 3L, 13L, 12L, 13L, 5L

not avoiding/skipping errors with try and tryCatch

我的未来我决定 提交于 2019-12-08 13:14:30
I have a nlsLM inside a for loop because I want to try different start values to fit my data. I already know that some start values generates this error : singular gradient matrix at initial parameter estimates , but I want to skip this error and continue with the loop , fitting the regression with the next start values . I tried to put all the for loop inside a try and a tryCatch block, setting silence=TRUE , but the code still stopping when the singular gradient matrix at initial parameter estimates error occurs. Someone can help me with that? Here is the code: try({ for (scp.loop in scp

Conditional nls fitting with dplyr+broom

我们两清 提交于 2019-12-08 09:01:37
问题 I am using the dplyr and broom combination and try to fitting regression models depending on the condition inside of the data groups. Finally I want to extract the regression coefficients by each group. So far I'm getting the same fitting results for all groups (Each group is separated with letters a:f ) . It's the main problem. library(dplyr) library(minpack.lm) library(broom) direc <- rep(rep(c("North","South"),each=20),times=6) V <- rep(c(seq(2,40,length.out=20),seq(-2,-40,length.out=20))

plotting nls fits with overlapping prediction intervals in a single figure

怎甘沉沦 提交于 2019-12-08 05:48:37
问题 Say I some data, d , and I fit nls models to two subsets of the data. x<- seq(0,4,0.1) y1<- (x*2 / (0.2 + x)) y1<- y1+rnorm(length(y1),0,0.2) y2<- (x*3 / (0.2 + x)) y2<- y2+rnorm(length(y2),0,0.4) d<-data.frame(x,y1,y2) m.y1<-nls(y1~v*x/(k+x),start=list(v=1.9,k=0.19),data=d) m.y2<-nls(y2~v*x/(k+x),start=list(v=2.9,k=0.19),data=d) I then want to plot the fitted model regression line over data, and shade the prediction interval. I can do this with the package investr and get nice plots for each

nls and log scale in ggplot2

て烟熏妆下的殇ゞ 提交于 2019-12-08 05:09:59
问题 I'm trying to plot 3 non-linear models in ggplot2. It's working in automatic scale but not in log10 scale where I get the "singular gradient error". What could be the problem(s)? The data I'm trying to fit (df1): x y 4.17 0.55 10.08 0.48 40.25 0.38 101.17 0.32 400.33 0.24 The code I tried: plot <- ggplot(df1, aes(x=x, y=y))+ stat_smooth(method="nls", formula=y~I(a*x^(-n)), data=df1, start=list(a=1,n=1), se=FALSE, colour="red")+ stat_smooth(method="nls", formula=y~m*(x+m^(1/n))^(-n), data=df1,