[R] adding regression curve to xyplot

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 30 17:24:44 CET 2004


On Tue, 30 Nov 2004, Carlisle Thacker wrote:

> Dear R-listers,
>
> It seems that predict() behaves differently within panel.xyplot.  Am I
> doing something stupid?

You fitted the model lm(x ~ y) and supplied new values for t, not y.

Using panel.abline would be a bit easier: just call panel.abline(thislm) 
from your panel function.

> Thanks,
>
> Carlisle
>
> First, without xyplot():
>> lmtest <- lm(t~s,data=subset(P100,whichLon100==1 & whichLat100==1))
>> lmtest
>
> Call:
> lm(formula = s ~ t, data = subset(P100, whichLon100 == 1 & whichLat100 ==
>   1))
>
> Coefficients:
> (Intercept)            t
>    33.3307       0.1393
>> range(P100$t)
> [1]  4.050469 24.514543
>>> predict(lmtest,newdata=data.frame(t=range(P100$t)))
>       1        2
> 33.89501 36.74620
>
> As expected, predict gives two values.  But inside xyplot() predict gives
> 300 values:
>
>> xyplot(t~s|factor(lonLabels[whichLon100])*factor(latLabels[whichLat100]),
> +   data=P100,pch=".",
> +   panel=function(x,y,...){panel.xyplot(x,y,...)
> +                           thislm <- lm(x~y)
> +                      print(thislm)
> +                           newt <- range(P100$t)
> +                      print(newt)
> +                           news <-
> as.vector(predict(thislm,newdata=data.frame(t=newt)))
> +                      print(news)
> +                           llines(news,newt,col="red")})
>
> Call:
> lm(formula = x ~ y)
>
> Coefficients:
> (Intercept)            y
>    33.3307       0.1393
>
> [1]  4.050469 24.514543
>  [1] 34.16173 35.31284 34.95317 35.62314 34.64448 34.06225 34.19688 34.03391
>  [9] 33.90860 35.28849 35.29911 34.61618 35.19837 35.28966 34.91978 35.32388
> [17] 35.39351 35.54533 35.58760 34.77039 35.07226 35.35643 35.40594 33.98609
> [25] 33.96461 33.91158 34.12389 34.33804 34.72235 34.63541 36.20420 36.13635
> [33] 34.60623 34.32401 35.59768 34.68576 34.64661 34.58804 34.74548 35.09062
> [41] 34.70290 34.22428 35.12743 34.90726 34.52325 34.90041 35.46984 34.79572
> [49] 35.47026 35.35481 34.70722 34.08883 33.97586 33.94797 33.96165 33.97715
> [57] 33.94886 33.99715 34.10887 34.72597 34.20534 34.45016 35.00769 34.56742
> [65] 34.76119 34.87190 34.60760 34.50113 34.56594 34.65954 34.57076 34.91072
> [73] 34.97322 34.99036 34.48229 34.58436 34.56767 34.94204 34.95929 34.89107
> [81] 34.98846 34.51328 34.74252 34.61337 34.62212 34.70953 34.74080 34.71630
> [89] 34.68599 35.58629 34.77031 34.49366 34.49873 34.48112 35.11327 34.62598
> [97] 34.50436 34.50705 34.70170 35.01278 35.12273 35.15541 35.06295 35.15366
> [105] 35.15089 35.11312 35.09834 35.05271 34.84838 34.52225 34.34650 34.32637
> [113] 34.42911 34.79282 35.06830 35.11559 35.25736 34.43281 34.45208 34.58407
> [121] 35.28598 34.34246 34.32532 34.47483 34.79309 34.02772 34.01553 35.33372
> [129] 34.19273 34.07622 34.52820 35.27539 35.37703 34.70560 34.77501 34.52084
> [137] 34.68773 35.19217 35.69538 34.21886 34.17226 34.94784 34.03079 33.95669
> [145] 33.92002 33.92972 33.96568 33.92996 33.94236 33.93887 33.92059 33.91746
> [153] 33.98369 34.17190 34.05327 33.96500 34.16136 33.98172 34.02463 34.00802
> [161] 34.05238 34.05088 34.16828 34.14888 33.94711 33.96567 34.12450 33.98988
> [169] 33.98670 33.98489 34.05447 34.03689 34.17137 34.31777 34.05027 33.97821
> [177] 33.99281 34.00209 34.07560 33.98840 33.98930 33.98548 33.99127 35.23652
> [185] 35.17034 35.53546 35.53861 34.02384 34.03402 33.93877 33.93540 33.90636
> [193] 33.92267 34.57678 34.49457 34.46038 34.64318 35.06561 35.06680 34.76466
> [201] 34.00418 35.48020 35.12687 35.28180 35.07281 35.23499 35.14693 34.07914
> [209] 33.99047 34.00648 34.10073 34.00784 33.99185 33.98191 34.03089 33.96755
> [217] 34.17042 34.10803 34.12671 34.09942 34.25003 34.06542 34.07293 34.19048
> [225] 34.06594 34.01929 34.00155 34.11555 33.89754 33.89527 33.89501 33.93433
> [233] 33.89881 33.94577 33.96603 33.95199 34.00284 34.12974 33.98116 34.08882
> [241] 34.00953 34.98977 34.72824 34.68920 35.15207 34.59716 34.64580 34.25087
> [249] 34.96008 34.62607 35.32346 35.16993 34.46759 34.47157 34.46978 34.46092
> [257] 34.46561 34.46489 34.49970 34.49599 34.55441 34.52048 34.58480 34.59267
> [265] 34.50082 34.57311 34.60421 34.57043 34.53131 34.46365 34.41946 34.74566
> [273] 34.64593 34.67921 34.65706 34.71625 34.61487 34.61687 34.58899 34.47081
> [281] 34.58366 35.66823 35.61883 34.71477 34.72518 34.69732 34.66265 34.65186
> [289] 34.81468 34.74725 34.77658 34.77191 34.81481 34.76427 34.70751 34.96763
> [297] 35.11322 35.16852 34.94393 34.89605
> Error in xy.coords(x, y) : x and y lengths differ
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list