[19194] in s-news-athena
[S] panel.abline() for weighted regression lines
daemon@ATHENA.MIT.EDU (John Thaden)
Sun Sep 12 14:39:05 1999
Message-Id: <3.0.3.32.19990912123117.006f27a4@pop.flash.net>
Date: Sun, 12 Sep 1999 12:31:17 -0500
To: s-news@wubios.wustl.edu
From: John Thaden <jjthaden@flash.net>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
In S-Plus v. 4.5 for Windows, I can't figure out how to draw weighted
regression lines on Trellis graphics xyplot() panels. Another complication
is that I want my y axis to be logarithmic and the lines to be log-linear
fits. Any suggestions?
Here's a simple dataframe, two examples of unweighted fit lines, and two
failed attempts at weighting the lines:
mydata <- data.frame(out = c(1,12,99,1,8,101),
in = c(3,2,1,3,2,1), strat = c(1,1,1,2,2,2),
wgts = c(1, 0.5, 1, 1, 0.5, 1))
print(mydata) # Given the dataframe above . . .
xyplot(I(log10(out))~in|strat, mydata,
panel = function(x,y,...){
panel.xyplot(x,y,...)
panel.abline(lm(y~x),...)
}) # this plot has unweighted regression line
xyplot(out~in|strat, mydata,
scales = list(y=list(log = 10)),
panel = function(x,y,w,...){
panel.xyplot(x,y)
coefs <- coef(lm(y~x))
x.temp <- c(min(x), max(x))
y.temp <- coefs[1] + coefs[2]*x.temp
lines(x.temp, y.temp)
}) # and so does this one, via the scales= argument.
xyplot(I(log10(out))~in|strat, mydata,
panel = function(x,y,w,...){
panel.xyplot(x,y)
panel.abline(lm(y~x, weights = w),...)
}, w = mydata$wgts) # But trying to weight the regression
#gives . . .
#Error in panel(x = structure(.Data = c(3, 2, ..: Length of variable 3
#is 6 != length of row names (3)
#Dumped
xyplot(out~in|strat, mydata, scales = list(y=list(log = 10)),
panel = function(x,y,w,...){
panel.xyplot(x,y)
coefs <- coef(lm(y~x, weights = w))
x.temp <- c(min(x), max(x))
y.temp <- coefs[1] + coefs[2]*x.temp
lines(x.temp, y.temp)
}, w = mydata$wgts) # and likewise here . . .
#Error in panel(x = structure(.Data = c(3, 2, ..: Length of variable 3
#is 6 != length of row names (3)
#Dumped
I've also tried fooling with setup.2d.trellis() and other accessory
functions, to no avail. Any help would be appreciated greatly.
John Thaden
Little Rock, Arkansas, USA
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news