I am trying to write a simple iterative zero-square least-square algorithm in R. I want to pass a function as an argument to calculate the weights, but unfortunately R complains that the function cannot be found. Any ideas what I'm doing wrong? Thanks in advance!
Here is my code:
irls <- function(imodel, wfunc, tol) { repeat { b0 <- imodel$coef imodel <- lm(formula(imodel), weights=wfunc(imodel), data=imodel$model) b1 <- imodel$coef if(abs((b1-b0)/b0)<=tol) break } imodel }
and stupid example to demonstrate the problem
x <- 1:100 y <- x + rnorm(100) mlm <- lm(y~x-1) irls(mlm, function(x){rep(1,length(x$fit))},0.001) # error: wfunc not found
function iteration r regression
linuxfever
source share