predict.lmrob: Predict method for Robust Linear Model ("lmrob") Fits

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/lmrob.R View source: R/lmrobPredict.R

Description

Predicted values based on robust linear model object.

Usage

1
2
3
4
5
6
## S3 method for class 'lmrob'
predict(object, newdata, se.fit = FALSE,
       scale = NULL, df = NULL,
       interval = c("none", "confidence", "prediction"), level = 0.95,
       type = c("response", "terms"), terms = NULL,
       na.action = na.pass, pred.var = res.var/weights, weights = 1, ...)

Arguments

object

object of class inheriting from "lmrob"

newdata

an optional data frame in which to look for variables with which to predict. If omitted, the fitted values are used.

se.fit

a switch indicating if standard errors are required.

scale

scale parameter for std.err. calculation

df

degrees of freedom for scale

interval

type of interval calculation.

level

tolerance/confidence level

type

Type of prediction (response or model term).

terms

if type="terms", which terms (default is all terms)

na.action

function determining what should be done with missing values in newdata. The default is to predict NA.

pred.var

the variance(s) for future observations to be assumed for prediction intervals. See ‘Details’.

weights

variance weights for prediction. This can be a numeric vector or a one-sided model formula. In the latter case, it is interpreted as an expression evaluated in newdata

...

further arguments passed to or from other methods.

Details

Note that this lmrob method for predict is closely modeled after the method for lm(), predict.lm, maybe see there for caveats with missing value treatment. The prediction intervals are for a single observation at each case in newdata (or by default, the data used for the fit) with error variance(s) pred.var. This can be a multiple of res.var, the estimated value of σ^2: the default is to assume that future observations have the same error variance as those used for fitting. If weights is supplied, the inverse of this is used as a scale factor. For a weighted fit, if the prediction is for the original data frame, weights defaults to the weights used for the model fit, with a warning since it might not be the intended result. If the fit was weighted and newdata is given, the default is to assume constant prediction variance, with a warning.

Value

predict.lmrob produces a vector of predictions or a matrix of predictions and bounds with column names fit, lwr, and upr if interval is set. If se.fit is TRUE, a list with the following components is returned:

fit

vector or matrix as above

se.fit

standard error of predicted means

residual.scale

residual standard deviations

df

degrees of freedom for residual

Author(s)

Andreas Ruckstuhl

See Also

lmrob and the (non-robust) traditional predict.lm method.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
## Predictions --- artificial example -- closely following  example(predict.lm)

set.seed(5)
n <- length(x <- sort(c(round(rnorm(25), 1), 20)))
y <- x + rnorm(n)
iO <- c(sample(n-1, 3), n)
y[iO] <- y[iO] + 10*rcauchy(iO)

p.ex <- function(...) {
  plot(y ~ x, ...); abline(0,1, col="sky blue")
  points(y ~ x, subset=iO, col="red", pch=2)
  abline(lm   (y ~ x), col = "gray40")
  abline(lmrob(y ~ x), col = "forest green")
  legend("topleft", c("true", "Least Squares", "robust"),
         col = c("sky blue", "gray40", "forest green"), lwd=1.5, bty="n")
}
p.ex()

fm <- lmrob(y ~ x)
predict(fm)
new <- data.frame(x = seq(-3, 10, 0.25))
str(predict(fm, new, se.fit = TRUE))
pred.w.plim <- predict(fm, new, interval = "prediction")
pred.w.clim <- predict(fm, new, interval = "confidence")
pmat <- cbind(pred.w.clim, pred.w.plim[,-1])

matlines(new$x, pmat, lty = c(1,2,2,3,3))# add to first plot
## show zoom-in region :
rect(xleft = -3, ybottom = -20, xright = 10, ytop = 40,
     lty = 3, border="orange4")

## now zoom in :
p.ex(xlim = c(-3,10), ylim = c(-20, 40))
matlines(new$x, pmat, lty = c(1,2,2,3,3))
box(lty = 3, col="orange4", lwd=3)
legend("bottom", c("fit", "lwr CI", "upr CI", "lwr Pred.I", "upr Pred.I"),
       col = 1:5, lty=c(1,2,2,3,3), bty="n")

## Prediction intervals, special cases
##  The first three of these throw warnings
w <- 1 + x^2
fit <- lmrob(y ~ x)
wfit <- lmrob(y ~ x, weights = w)
predict(fit,       interval = "prediction")
predict(wfit,      interval = "prediction")
predict(wfit, new, interval = "prediction")
predict(wfit, new, interval = "prediction", weights = (new$x)^2) -> p.w2
p.w2
stopifnot(identical(p.w2, ## the same as using formula:
     predict(wfit, new, interval = "prediction", weights = ~x^2)))

Example output

          1           2           3           4           5           6 
-1.53013833 -0.82011584 -0.66233307 -0.66233307 -0.42565891 -0.42565891 
          7           8           9          10          11          12 
-0.26787613 -0.26787613 -0.26787613 -0.18898475 -0.03120197 -0.03120197 
         13          14          15          16          17          18 
 0.04768942  0.12658080  0.28436358  0.28436358  0.36325496  0.75771190 
         19          20          21          22          23          24 
 0.83660329  0.91549467  0.91549467  1.15216883  1.30995161  1.38884300 
         25          26 
 1.54662577 15.98374960 
List of 4
 $ fit           : Named num [1:53] -2.16 -1.96 -1.77 -1.57 -1.37 ...
  ..- attr(*, "names")= chr [1:53] "1" "2" "3" "4" ...
 $ se.fit        : Named num [1:53] 0.672 0.626 0.581 0.536 0.493 ...
  ..- attr(*, "names")= chr [1:53] "1" "2" "3" "4" ...
 $ df            : int 24
 $ residual.scale: num 1.85
           fit       lwr       upr
1  -1.53013833 -5.502382  2.442105
2  -0.82011584 -4.720235  3.080003
3  -0.66233307 -4.551090  3.226424
4  -0.66233307 -4.551090  3.226424
5  -0.42565891 -4.300626  3.449308
6  -0.42565891 -4.300626  3.449308
7  -0.26787613 -4.135836  3.600084
8  -0.26787613 -4.135836  3.600084
9  -0.26787613 -4.135836  3.600084
10 -0.18898475 -4.054102  3.676132
11 -0.03120197 -3.891957  3.829553
12 -0.03120197 -3.891957  3.829553
13  0.04768942 -3.811549  3.906928
14  0.12658080 -3.731584  3.984745
15  0.28436358 -3.572985  4.141712
16  0.28436358 -3.572985  4.141712
17  0.36325496 -3.494351  4.220861
18  0.75771190 -3.107834  4.623258
19  0.83660329 -3.031857  4.705063
20  0.91549467 -2.956320  4.787309
21  0.91549467 -2.956320  4.787309
22  1.15216883 -2.732338  5.036675
23  1.30995161 -2.585192  5.205095
24  1.38884300 -2.512267  5.289953
25  1.54662577 -2.367702  5.460954
26 15.98374960  6.886912 25.080588
Warning message:
In predict.lmrob(fit, interval = "prediction") :
  Predictions on current data refer to _future_ responses
            fit         lwr        upr
1   -4.30551194  -7.4943735  -1.116650
2   -1.63591308  -4.7856137   1.513788
3   -1.22372603  -4.5844892   2.137037
4   -1.22372603  -4.5844892   2.137037
5   -0.71941317  -4.5158797   3.077053
6   -0.71941317  -4.5158797   3.077053
7   -0.45189959  -4.5842611   3.680462
8   -0.45189959  -4.5842611   3.680462
9   -0.45189959  -4.5842611   3.680462
10  -0.33582097  -4.6343678   3.962726
11  -0.13165310  -4.7194434   4.456137
12  -0.13165310  -4.7194434   4.456137
13  -0.03973921  -4.7320931   4.652615
14   0.04840603  -4.7102986   4.807111
15   0.22354180  -4.5334089   4.980492
16   0.22354180  -4.5334089   4.980492
17   0.31569631  -4.3729937   5.004386
18   0.90967184  -3.0313500   4.850694
19   1.06594795  -2.7018569   4.833753
20   1.23705913  -2.3640808   4.838199
21   1.23705913  -2.3640808   4.838199
22   1.84462275  -1.3260931   5.015339
23   2.33151456  -0.6275329   5.290562
24   2.60014080  -0.2787054   5.478987
25   3.18836498   0.4155862   5.961144
26 351.67795993 203.1819257 500.173994
Warning messages:
1: In predict.lmrob(wfit, interval = "prediction") :
  Predictions on current data refer to _future_ responses
2: In predict.lmrob(wfit, interval = "prediction") :
  Assuming prediction variance inversely proportional to weights used for fitting
           fit         lwr       upr
1  -2.47869992 -7.41581667  2.458417
2  -2.26086667 -7.17530165  2.653568
3  -2.04303342 -6.93648361  2.850417
4  -1.82520017 -6.69938449  3.048984
5  -1.60736691 -6.46402473  3.249291
6  -1.38953366 -6.23042323  3.451356
7  -1.17170041 -5.99859722  3.655196
8  -0.95386716 -5.76856217  3.860828
9  -0.73603391 -5.54033174  4.068264
10 -0.51820065 -5.31391766  4.277516
11 -0.30036740 -5.08932970  4.488595
12 -0.08253415 -4.86657559  4.701507
13  0.13529910 -4.64566099  4.916259
14  0.35313235 -4.42658946  5.132854
15  0.57096560 -4.20936244  5.351294
16  0.78879886 -3.99397922  5.571577
17  1.00663211 -3.78043696  5.793701
18  1.22446536 -3.56873074  6.017661
19  1.44229861 -3.35885351  6.243451
20  1.66013186 -3.15079620  6.471060
21  1.87796512 -2.94454775  6.700478
22  2.09579837 -2.74009516  6.931692
23  2.31363162 -2.53742356  7.164687
24  2.53146487 -2.33651631  7.399446
25  2.74929812 -2.13735509  7.635951
26  2.96713138 -1.93991995  7.874183
27  3.18496463 -1.74418948  8.114119
28  3.40279788 -1.55014084  8.355737
29  3.62063113 -1.35774993  8.599012
30  3.83846438 -1.16699149  8.843920
31  4.05629763 -0.97783915  9.090434
32  4.27413089 -0.79026565  9.338527
33  4.49196414 -0.60424285  9.588171
34  4.70979739 -0.41974190  9.839337
35  4.92763064 -0.23673334 10.091995
36  5.14546389 -0.05518719 10.346115
37  5.36329715  0.12492694 10.601667
38  5.58113040  0.30363976 10.858621
39  5.79896365  0.48098220 11.116945
40  6.01679690  0.65698530 11.376609
41  6.23463015  0.83168018 11.637580
42  6.45246341  1.00509793 11.899829
43  6.67029666  1.17726951 12.163324
44  6.88812991  1.34822574 12.428034
45  7.10596316  1.51799721 12.693929
46  7.32379641  1.68661422 12.960979
47  7.54162966  1.85410674 13.229153
48  7.75946292  2.02050437 13.498421
49  7.97729617  2.18583627 13.768756
50  8.19512942  2.35013116 14.040128
51  8.41296267  2.51341727 14.312508
52  8.63079592  2.67572234 14.585870
53  8.84862918  2.83707353 14.860185
Warning message:
In predict.lmrob(wfit, new, interval = "prediction") :
  Assuming constant prediction variance even though model fit is weighted
           fit         lwr        upr
1  -2.47869992  -4.5393655 -0.4180344
2  -2.26086667  -4.3824600 -0.1392733
3  -2.04303342  -4.2620779  0.1760110
4  -1.82520017  -4.1886422  0.5382418
5  -1.60736691  -4.1770882  0.9623544
6  -1.38953366  -4.2502295  1.4711621
7  -1.17170041  -4.4456041  2.1022033
8  -0.95386716  -4.8306097  2.9228753
9  -0.73603391  -5.5403317  4.0682639
10 -0.51820065  -6.8909225  5.8545212
11 -0.30036740  -9.8328797  9.2321449
12 -0.08253415 -19.1235479 18.9584796
13  0.13529910        -Inf        Inf
14  0.35313235 -18.6867965 19.3930613
15  0.57096560  -8.9572119 10.0991431
16  0.78879886  -5.5741916  7.1517894
17  1.00663211  -3.7804370  5.7937012
18  1.22446536  -2.6255441  5.0744749
19  1.44229861  -1.7935281  4.6781253
20  1.66013186  -1.1495652  4.4698289
21  1.87796512  -0.6266252  4.3825555
22  2.09579837  -0.1876314  4.3792281
23  2.31363162   0.1897108  4.4375525
24  2.53146487   0.5198168  4.5431129
25  2.74929812   0.8126515  4.6859448
26  2.96713138   1.0755263  4.8587364
27  3.18496463   1.3140705  5.0558587
28  3.40279788   1.5327638  5.2728320
29  3.62063113   1.7352383  5.5060239
30  3.83846438   1.9244614  5.7524673
31  4.05629763   2.1028592  6.0097361
32  4.27413089   2.2724100  6.2758518
33  4.49196414   2.4347223  6.5492060
34  4.70979739   2.5910998  6.8284950
35  4.92763064   2.7425976  7.1126637
36  5.14546389   2.8900690  7.4008588
37  5.36329715   3.0342051  7.6923892
38  5.58113040   3.1755661  7.9866947
39  5.79896365   3.3146079  8.2833194
40  6.01679690   3.4517023  8.5818915
41  6.23463015   3.5871536  8.8821067
42  6.45246341   3.7212124  9.1837144
43  6.67029666   3.8540854  9.4865080
44  6.88812991   3.9859440  9.7903159
45  7.10596316   4.1169311 10.0949953
46  7.32379641   4.2471662 10.4004266
47  7.54162966   4.3767499 10.7065095
48  7.75946292   4.5057668 11.0131590
49  7.97729617   4.6342889 11.3203034
50  8.19512942   4.7623773 11.6278815
51  8.41296267   4.8900843 11.9358410
52  8.63079592   5.0174547 12.2441372
53  8.84862918   5.1445270 12.5527314

robustbase documentation built on May 30, 2017, 12:48 a.m.