Question

I need to run a non-linear least squares regression on an entire data set, and then repeat the regression on several subsets of that data set. I can do this for a single subset; for example (where y is a generic logistic equation, and x is a vector from 1 to 20):

example = nls(x ~ y, subset = c(2:20))

but I want to do this for 3:20, 4:20, 5:20, etc. I tried a for loop:

datasubsets <- sapply(2:19, seq, to = 20)
for (i in 1:19){
    example[i] = nls(x ~ y, subset = datasubsets[i])
}

but I receive "Error in xj[i] : invalid subscript type 'list'". I would very much like to avoid having to copy and paste nls() 20 times. Any help is much appreciated.

Was it helpful?

Solution

This does the job: sapply(2:19,function(jj) nls(x~y,subset=jj:20)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top