R/predict.msc.lm.R

Defines functions predict.msc.lm

Documented in predict.msc.lm

predict.msc.lm <- function (object, newdata, ...) 
{
    ms = object$ms 
    msLevel = ms$level[[ms$predictLevel]]
    lms = object$lms[[ms$predictLevel]]

    x <- model.matrix(ms, newdata)

    colnames(x) <- colnames(ms$x)
    x <- as.data.frame(x)
 
    #predict model for each crystals 
    f = matrix(ncol=length(msLevel$mins), nrow=nrow(x));
    for(i in 1:length(lms$lm)){
      f[,i] = predict(lms$lm[[i]], x)
    }
    
    #piecewise linear or blending of the models
    d <- predict(ms, x)
    if(object$blend){ 
      f <- f * d
      predicted <- rowSums(f)
    }
    else{
      predicted <- vector(mode = "double", length = nrow(f)) 
      for(i in 1:nrow(f)){
        ind = which.max(d[i,])
        predicted[i] = f[i, ind]
      }
    }
    predicted

}

Try the msr package in your browser

Any scripts or data that you put into this service are public.

msr documentation built on May 30, 2017, 4:23 a.m.