[R] lines and points in xyplot()

2011-11-23 Thread Doran, Harold
Given the following data, I want a scatterplot with the data points and the 
predictions from the regression.

Sigma - matrix(c(1,.6,1,.6), 2)
mu - c(0,0)
dat - mvrnorm(5000, mu, Sigma)

x - dat[,1] * 50 + 200
y - dat[,2] * 50 + 200

fm - lm(y ~ x)

### This gives the regression line, but not the data
xyplot(y ~ x,
   type = c('g', 'p'),
   panel = function(x, y){
   panel.lines(x, predict(fm))
   }
)

### This gives both data but as point
xyplot(y + predict(fm) ~ x,
   type = c('g', 'p'),
   )

I know I can add an abline easily, but my problem is a bit more complex and the 
code above is just an example. What is the best way for the predicted data to 
form a solid line and let the data points remain as points

Harold

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] lines and points in xyplot()

2011-11-23 Thread Deepayan Sarkar
On Wed, Nov 23, 2011 at 10:48 PM, Doran, Harold hdo...@air.org wrote:
 Given the following data, I want a scatterplot with the data points and the 
 predictions from the regression.

 Sigma - matrix(c(1,.6,1,.6), 2)
 mu - c(0,0)
 dat - mvrnorm(5000, mu, Sigma)

 x - dat[,1] * 50 + 200
 y - dat[,2] * 50 + 200

 fm - lm(y ~ x)

 ### This gives the regression line, but not the data
 xyplot(y ~ x,
               type = c('g', 'p'),
               panel = function(x, y){
               panel.lines(x, predict(fm))
               }
 )

 ### This gives both data but as point
 xyplot(y + predict(fm) ~ x,
               type = c('g', 'p'),
               )

 I know I can add an abline easily, but my problem is a bit more complex and 
 the code above is just an example.
 What is the best way for the predicted data to form a solid line and let the 
 data points remain as points

See

http://lattice.r-forge.r-project.org/Vignettes/src/lattice-tricks/regression-lines.pdf

(This is a work in progress, so feedback would be appreciated.)

-Deepayan

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] lines and points in xyplot()

2011-11-23 Thread Dennis Murphy
Hi:

Try this:

library('lattice')
xyplot(y ~ x,
  type = c('g', 'p'),
  panel = function(x, y, ...){
  panel.xyplot(x, y, ...)
  panel.lines(x, predict(fm), col = 'black', lwd = 2)
  }
 )

HTH,
Dennis

On Wed, Nov 23, 2011 at 9:18 AM, Doran, Harold hdo...@air.org wrote:
 Given the following data, I want a scatterplot with the data points and the 
 predictions from the regression.

 Sigma - matrix(c(1,.6,1,.6), 2)
 mu - c(0,0)
 dat - mvrnorm(5000, mu, Sigma)

 x - dat[,1] * 50 + 200
 y - dat[,2] * 50 + 200

 fm - lm(y ~ x)

 ### This gives the regression line, but not the data
 xyplot(y ~ x,
               type = c('g', 'p'),
               panel = function(x, y){
               panel.lines(x, predict(fm))
               }
 )

 ### This gives both data but as point
 xyplot(y + predict(fm) ~ x,
               type = c('g', 'p'),
               )

 I know I can add an abline easily, but my problem is a bit more complex and 
 the code above is just an example. What is the best way for the predicted 
 data to form a solid line and let the data points remain as points

 Harold

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.