0

I'd like to predict values using a generated model. That is the simple part:

predicted = fitted.values(glm(dep ~ indep, family = myFamily, maxit = myMaxit)

But: for each case I don't want to use that case for building that model (without using a for-loop)

Example:

Grade  Sex  Age  Course  School
-------------------------------
  1    m    11   math    St.Adam
  2    w    12   engl    St.Adam
  3    m    13   fren    St.Adam
  4    w    14   math    St.Eve
  5    m    15   engl    St.Eve
  6    w    16   fren    St.Eve
    …   …     …     …        …

Assume I want to predict a mean grade for St.Adam's pupils but don't want to use them for building the model.

Hoffmann
  • 1,050
  • 9
  • 26
  • So you are essentially trying to do a form of leave-one out validation/prediction? Can you give a little more context, and a reproducible example? Can you explain why you would like to avoid loops? (In principle you could create a function that would try to efficiently update the model by the omission/addition of single cases, but I don't see how you're going to avoid loops entirely ...) – Ben Bolker May 28 '13 at 15:49
  • I learned that loops in "languages" like Matlab, R, etc. are usually slower so I'm looking for a "native" solution. If a for loop is a native solution I'm sorry for the misunderstanding. – Hoffmann May 29 '13 at 10:38
  • "loops are slow" is a slight overgeneralization, although not bad as a first pass. A better statement is "vectorized solutions are much faster, when they work". In this case I don't think there's an easy way to create a vectorized solution. – Ben Bolker May 29 '13 at 13:16

1 Answers1

1

Maybe something like...

lapply(1:dim(df)[1], FUN = function(x)
    fitted.values(glm(dep ~ indep, family = myFamily, maxit = myMaxit, data=df[-x,])) )
Thomas
  • 43,637
  • 12
  • 109
  • 140