nls

`nls` fitting error: always reach maximum number of iterations regardless starting values

亡梦爱人 提交于 2019-12-04 01:55:06
问题 Using this parametrization for a growth curve logistic model I created some points with: K =0.7 ; y0=0.01 ; r =0.3 df = data.frame(x= seq(1, 50, by = 5)) df$y = 0.7/(1+((0.7-0.01)/0.01)*exp(-0.3*df$x)) Can someone tell me how can I have a fitting error if create the data with the model starters? fo = df$y ~ K/(1+((K-y0)/y0)*exp(-r*df$x)) model<-nls(fo, start = list(K=0.7, y0=0.01, r=0.3), df, nls.control(maxiter = 1000)) Error in nls(fo, start = list(K = 0.7, y0 = 0.01, r = 0.3), df, nls

R: polynomial shortcut notation in nls() formula

二次信任 提交于 2019-12-03 21:35:12
With the linear model function lm() polynomial formulas can contain a shortcut notation like this: m <- lm(y ~ poly(x,3)) this is a shortcut that keeps the user from having to create x^2 and x^3 variables or typing them in the formula like I(x^2) + I(x^3) . Is there comparable notation for the nonlinear function nls() ? poly(x, 3) is rather more than just a shortcut for x + I(x ^ 2) + I(x ^ 3) - it actually produces legendre polynomials which have the nice property of being uncorrelated: options(digits = 2) x <- runif(100) var(cbind(x, x ^ 2, x ^ 3)) # x # x 0.074 0.073 0.064 # 0.073 0.077 0

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

妖精的绣舞 提交于 2019-12-03 14:53:48
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 in the Equation above) and one called "values" (equivalent to the value of the function f(x) in the

Fitting a function in R

匆匆过客 提交于 2019-12-03 07:41:53
I have a few datapoints (x and y) that seem to have a logarithmic relationship. > mydata x y 1 0 123 2 2 116 3 4 113 4 15 100 5 48 87 6 75 84 7 122 77 > qplot(x, y, data=mydata, geom="line") Now I would like to find an underlying function that fits the graph and allows me to infer other datapoints (i.e. 3 or 82 ). I read about lm and nls but I'm not getting anywhere really. At first, I created a function of which I thought it resembled the plot the most: f <- function(x, a, b) { a * exp(b *-x) } x <- seq(0:100) y <- f(seq(0:100), 1,1) qplot(x,y, geom="line") Afterwards, I tried to generate a

Can we make prediction with nlxb from nlmrt package?

我的梦境 提交于 2019-12-02 11:17:16
I'm asking this question because I couldn't figure it out why nlxb fitting function does not work with the predict() function. I have been looking around to solve this but so far no luck:( I use dplyr to group data and use do to fit each group using nlxb from nlmrt package. Here is my attempt set.seed(12345) set =rep(rep(c("1","2","3","4"),each=21),times=1) time=rep(c(10,seq(100,900,100),seq(1000,10000,1000),20000),times=1) value <- replicate(1,c(replicate(4,sort(10^runif(21,-6,-3),decreasing=FALSE)))) data_rep <- data.frame(time, value,set) > head(data_rep) # time value set #1 10 1.007882e-06

`nls` fails to estimate parameters of my model

余生颓废 提交于 2019-12-02 10:25:18
问题 I am trying to estimate the constants for Heaps law. I have the following dataset novels_colection : Number of novels DistinctWords WordOccurrences 1 1 13575 117795 2 1 34224 947652 3 1 40353 1146953 4 1 55392 1661664 5 1 60656 1968274 Then I build the next function: # Function for Heaps law heaps <- function(K, n, B){ K*n^B } heaps(2,117795,.7) #Just to test it works So n = Word Occurrences , and K and B are values that should be constants in order to find my prediction of Distinct Words. I

How do I run an exponential nls with seasonal dummies in R?

人盡茶涼 提交于 2019-12-02 09:17:44
问题 I'm having trouble with running an nls regression with seasonal dummies in R. I'm able to do it without the seasonal dummies, but not with. This is what I have so far: year=floor(time(lsts)) > month=round(time(lsts)-year,4) > month.f=factor(month) > dummies=model.matrix(~month.f) hotdogNLS<-nls(lsts~beta1/(1+exp(beta2+beta3*t)),start=list(beta1=2500,beta2=0.5,beta3=-0.5),trace=F) summary(hotdogNLS) Formula: lsts ~ beta1/(1 + exp(beta2 + beta3 * t)) Parameters: Estimate Std. Error t value Pr(>

Error: Results are not data frames at positions:

∥☆過路亽.° 提交于 2019-12-02 07:06:11
I am trying to run a fitting function on a rather large data frame, grouped by a variable named "big_group" and 'small_group' . In particular, I am trying to get predictions and coefs values of every small_group inside of big_group . That is, I'm trying to add these new columns to my new data.frame at the end of do({ function. Some of the groups of this data cannot be fitted due to either lack of data points or "singular gradient matrix at initial parameter estimates" error. So, I used tryCatch method from this post of how-do-i-ignore-errors-and-continue-processing-list-items and I used

`nls` fails to estimate parameters of my model

守給你的承諾、 提交于 2019-12-02 06:15:19
I am trying to estimate the constants for Heaps law. I have the following dataset novels_colection : Number of novels DistinctWords WordOccurrences 1 1 13575 117795 2 1 34224 947652 3 1 40353 1146953 4 1 55392 1661664 5 1 60656 1968274 Then I build the next function: # Function for Heaps law heaps <- function(K, n, B){ K*n^B } heaps(2,117795,.7) #Just to test it works So n = Word Occurrences , and K and B are values that should be constants in order to find my prediction of Distinct Words. I tried this but it gives me an error: fitHeaps <- nls(DistinctWords ~ heaps(K,WordOccurrences,B), data =

Modifying a curve to prevent singular gradient matrix at initial parameter estimates

99封情书 提交于 2019-12-01 17:54:04
I want to use y=a^(b^x) to fit the data below, y <- c(1.0385, 1.0195, 1.0176, 1.0100, 1.0090, 1.0079, 1.0068, 1.0099, 1.0038) x <- c(3,4,5,6,7,8,9,10,11) data <- data.frame(x,y) When I use the non-linear least squares procedure, f <- function(x,a,b) {a^(b^x)} (m <- nls(y ~ f(x,a,b), data = data, start = c(a=1, b=0.5))) it produces an error: singular gradient matrix at initial parameter estimates. The result is roughly a = 1.1466, b = 0.6415, so there shouldn't be a problem with intial parameter estimates as I have defined them as a=1, b=0.5. I have read in other topics that it is convenient to