Edit nls or nlsLM function for waste calculation

Asked

Viewed 164 times

2

I need to adjust a non-linear function through routines nls or nlsLM, However, my data generates heterocedastic waste when calculated with these routines, which does not allow me to adjust directly through the sum of waste squares

I already have the equation that makes the residues homocedastic, but I do not know how to make the routine use this form to calculate the residues in place of the direct calculation of (observed-expected) 2

  • So Jose, do you have anything ready? Any excerpts from the code? Could tbm explain better what you mean by resíduos heterocedásticos and resíduos homocedásticos This seems to me to have more to do with Linear Regression -- Mathematics.

  • Hello, @Marcelobonifazio! I’m using the nlsLM function directly, just input my dataset and have it run. Heterocedastic residues occur when the difference between the observed and the expected (residues) presents a trend in their distribution over x. Routines {nls} and {nlsLM} adjust the model by minimizing the square of the waste, need to change this part of the code

  • Joseph, the code of nls is open and can be seen by typing nls on the R console. If you want to edit you can recreate the function by doing nls <- código mostrado... However, this should not be easy. There are other ways to fix heterocedasticity by changing the model and not code, are sure that’s what you need to do?

  • @Danielfalbel, I need to change the code because my data matrix has this problem. I even opened the nls code, but I could not identify the part that calculates the waste to make the change. You can tell me this?

  • I don’t think it pays to change the code for cost-effectiveness. Why don’t you try adjusting a model with a function for variance tbm? The function gnls adjusts models with a function for the variance passed to the argument weights

  • I tried to use the argument weights and varFunc in function gnls but I couldn’t make it work. I’m new to R and sometimes I get into simple problems. What is needed is to calculate the residues as follows: Resid = (observed - expected)/(Ax b). Parameters a and b I provide manually according to the data matrix used.

  • what is x in your formula?

  • x is my independent variable present in my data matrix. It occurs like this: I am relating weight according to the length of a species (x = length, y = weight), the variability in weight increases as individuals get bigger, so I divide the residues by an equation that describes this increase in variability (in this specific case, a power equation), thus eliminating heterocedasticity.

Show 3 more comments

1 answer

1


I made an example that should help you:

# dados do modelo
DNase1 <- subset(DNase, Run == 1)

# ajuste do mdoelo
fm1DNase1 <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1)

# definicao de a e b
a <- 1
b <- 2

# cálculo do resíduo
residuo <- (DNase1$density - predict(fm1DNase1))/((a*DNase1$density)^b)

In the last part of the code, I put the response variable in place of your x, but I don’t know if this is exactly what you wanted.

  • That’s the idea, but what I need to do is get the routine nls adjust the model using this equation for waste instead of simply (observed-expected) 2

  • could you post the template code that you are set? think it will get easier to help

  • Sure. This is the code I’m using to adjust the model: poly.2p.fitted<-gnls(Weight ~ (a1 * Length^b1) * (1/(1 + exp(rate * (Length -scp))))+(a2 * Length^b2) * (1-(1/(1 + exp(rate * (Length-scp))))), data=dados, start=list(a1=0.1,b1=2.5,a2=0.1,b2=3,rate=10,scp=11), control=gnlsControl(maxIter=99999))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.