Skip to content

add an automatized linear regression in a function

1 message · Rui Barradas

#
Em 04-05-2012 11:00, jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> escreveu:
Statistically speaking, I don't believe in what you want, but a solution 
could be

na.fill <- function(x, y){
     i <- is.na(x$data)
     xx <- y$data
     new <- data.frame(xx=xx)
     x$data[i] <- predict(lm(x$data~xx, na.action=na.exclude), new)[i]
     x
}

and in process.all, change function g() to

     g <- function(station){
         x <- df.list[[station]]
         if(any(is.na(x$data))){
             mat[row(mat) == col(mat)] <- -Inf
             nas <- which(is.na(x$data))
             ord <- order(mat[station, ], decreasing = TRUE)[-c(1, 
ncol(mat))]
             for(y in ord){
                 if(all(!is.na(df.list[[y]]$data[nas]))){
                     xx <- df.list[[y]]$data
                     new <- data.frame(xx=xx)
                     x$data[nas] <- predict(lm(x$data~xx, 
na.action=na.exclude), new)[nas]
                     break
                 }
             }
         }
         x
     }


Hope this helps,

Rui Barradas