Superimposing a line created with lqs

25 views
Skip to first unread message

Alan Arnholt

unread,
Feb 10, 2012, 4:37:05 PM2/10/12
to ggp...@googlegroups.com

Dear Group,

 

I am trying to superimpose lines computed with different strategies on a data set using rlm and lqs from the package MASS.  I can get the lm and rlm lines to show up in the graph but need some help figuring out what the object “weight” is… Here is what I am doing on a different data set that gives the same problem:

 

library(MASS)

p <- ggplot(data = msleep, aes(x = brainwt, y = bodywt))

#

lqs(bodywt~brainwt, data = msleep)

lm(bodywt~brainwt, data = msleep)

rlm(bodywt~brainwt, data = msleep)

pq <- p + geom_point() + stat_smooth(method = rlm, se = FALSE)

pq

pq1 <- pq + stat_smooth(method = lm, se = FALSE)

pq1

#  No problems until the next line….

pq1 + stat_smooth(method = lqs, se = FALSE)

# Partial output:

> pq1 + stat_smooth(method = lqs, se = FALSE) 

Error in lqs.control(...) : object 'weight' not found

In addition: Warning messages:

 

Any pointers to what lqs wants ….or how to superimpose a line fit using lqs would be most appreciated.  Thanks in advance.

 

Alan-

 

Winston Chang

unread,
Feb 12, 2012, 2:01:18 AM2/12/12
to Alan Arnholt, ggp...@googlegroups.com
I don't know what's causing your problem, but you can use predict() to make predictions from the model, and then plot them with geom_line.

# Make the model
lqsmod <- lqs(bodywt~brainwt, data = msleep)
# Generate a sequence of x (brainwt) values and put them into a data frame
xval <- seq(min(msleep$brainwt, na.rm=TRUE), max(msleep$brainwt, na.rm=TRUE),
            length.out=100)
preddata <- data.frame(brainwt = xval)
# Predict bodywt values
preddata$bodywt <- predict(lqsmod, newdata = preddata)
# print
preddata

pq1 + geom_line(data=preddata)

-Winston

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Reply all
Reply to author
Forward
0 new messages