Hello,

I am trying to print the lm 
coefficients using grid graphs (code below), and am getting all the 
coefficients 
on top of each other.

 

My aim is to put them as legend, 
showing both coefficients and mean values separated by colors as shown in 
xyplot.
 

testData <- data.frame(f1 = 
factor(rep(LETTERS[1:5], each = 20)),

                       f2 = 
factor(rep(letters[6:7], 50)),

                       x1 = 
rnorm(100, mean = 2, sd = 1),

                       x2 = 
rnorm(100, mean = 10, sd = 4),

                       x3 = 
rnorm(100, mean = 4, sd = 3),

                       y = (1:50) + 
rnorm(50, sd = 3) + rep(c(-2, 2), 50))

 

 

grid.newpage()

grps <- factor(testData[, 
"f2"])

xyplot( y ~ 
x1|f1,

       
groups=grps,

       panel=function(x, y, ...) 
{

         panel.superpose(x, y, 
...)

       
},

       panel.groups = function(x, y, 
...) {

         fit.lm <- 
lm(y~x)

         coef.fit <- 
round(coef(fit.lm)[-1], 2)

         avg.y <- round(mean(y), 
2)

         
panel.abline(fit.lm)

         panel.xyplot(x, y, 
...)

         ## Add lm 
coefficients:

         grid.text(x= unit(0.1, 
"npc"),

                   y= unit(seq(0.1, 
length= length(levels(grps)), by =0.1),

                     
"npc"),

                   label = 
paste(expression(beta), "=", coef.fit)

                
   ),
##     Add mean values: (note that "mu" is not shown as symbol)

grid.text(x= unit(0.1, 
"npc"),


                   y= unit(seq(0.9, 
length= length(levels(grps)), by =-0.1),


                     
"npc"),


                   label = 
paste(expression(mu), "=", avg.y)


                
   ),




       
},

       ##  Key 

       key = 
simpleKey(levels(grps),

         columns = 
2,

         space = 
"bottom",

         points = 
TRUE,

         lines = 
TRUE

         
),

       ## 
grid.legend()

       data= 
testData

     )

 



Thanks.

-NJ



      
____________________________________________________________________________________
Park yourself in front of a world of choices in alternative vehicles. Visit the 
Yahoo! Auto Green Center.

______________________________________________
R-help@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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to