Extract Observed Responses from New Data

Description

Generic function and methods for extracting response variables from new data based on fitted model objects.

Usage

newresponse(object, ...)

## Default S3 method:
newresponse(object, newdata, na.action = na.pass, ...)

## S3 method for class 'glm'
newresponse(object, newdata, na.action = na.pass, initialize = NULL, ...)

## S3 method for class 'distribution'
newresponse(object, newdata, ...)

Arguments

object a fitted model object. For the default method this needs to needs to be formula-based so that model.frame can be used to extract the response from the original data the model was fitted to or terms can be used to set up the response on newdata.
further arguments passed to methods.
newdata optionally, a data frame in which to look for variables with which to predict. If omitted, the original observations are used.
na.action function determining how to handle missing values in newdata, by default these are preserved.
initialize logical. Should the response variable from glm objects be initialized using the corresponding expression from the family? If NULL (the default), the initialization is only used for binomial and quasibinomial families.

Details

newresponse is a convenience function that supports functions like proscore or proresiduals which assess discrepancies between predictions/forecasts on new data and the corresponding observed response variables.

The default method takes an approach that is similar to many predict methods which rebuild the model.frame after dropping the response from the terms of a model object. However, here only the response variable is preserved and all explanatory variables are dropped. Missing values values are typically preserved (i.e., using na.pass).

If the new model.frame contains a variable “(weights)”, it is preserved along with the response variable(s).

A method for distribution objects is provided which expects that newdata is essentially already the corresponding new response. Thus, it needs to be a vector (or data frame) of the same length as distribution. If it is not a data frame, yet, it is transformed to one but no further modifications are made.

Value

A data.frame (model.frame) containing the response variable (and optionally a variable with “(weights)”).

See Also

terms, model.frame

Examples

library("topmodels")

## linear regression model
m <- lm(dist ~ speed, data = cars)

## extract response variable on data used for model fitting 
newresponse(m)
   dist
1     2
2    10
3     4
4    22
5    16
6    10
7    18
8    26
9    34
10   17
11   28
12   14
13   20
14   24
15   28
16   26
17   34
18   34
19   46
20   26
21   36
22   60
23   80
24   20
25   26
26   54
27   32
28   40
29   32
30   40
31   50
32   42
33   56
34   76
35   84
36   36
37   46
38   68
39   32
40   48
41   52
42   56
43   64
44   66
45   54
46   70
47   92
48   93
49  120
50   85
## extract response variable on "new" data
newresponse(m, newdata = cars[1:3, ])
  dist
1    2
2   10
3    4