Feeding newdata to R predict function

后端 未结 1 1015
悲哀的现实
悲哀的现实 2021-02-06 04:26

R\'s predict function can take a newdata parameter and its document reads:

newdata An optional data frame in which to look for v

1条回答
  •  梦谈多话
    2021-02-06 04:37

    See ?predict.lm and the Note section, which I quote below:

    Note:
    
         Variables are first looked for in ‘newdata’ and then searched for
         in the usual way (which will include the environment of the
         formula used in the fit).  A warning will be given if the
         variables found are not of the same length as those in ‘newdata’
         if it was supplied.
    

    Whilst it doesn't state the behaviour in terms of "same name" etc, as far as the formula is concerned the terms you passed in to it were of the form foo$var and there are no such variables with names like that either in newdata or along the search path that R will traverse to look for them.

    In your second case, you are totally misusing the model formula notation; the idea is to succinctly and symbolically describe the model. Succinctness and repeating the data object ad nauseum are not compatible.

    The behaviour you note is exactly consistent with the documented behaviour. In simple terms, you fitted the model with terms data$x and data$y then tried to predict for terms x and y. As far as R is concerned those are different names and hence different things and it did right to not match them.

    0 讨论(0)
提交回复
热议问题