0

I got a lm function from a dataframe with two groups. I am facet_wrapping one of them. IN each facet_wrap plot I would like to show the function of the lm.

I have this one (and it works):

lm_eqn = function(DAT){
  m = lm(Percentage ~ as.numeric(Year), DAT);
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));                 
}
eq <- ddply(DAT,.(Class),lm_eqn)

Now i would like to add the p- value inside the lots aswel..so I wat to use this code:

lm_eqn = function(DAT){
  m = lm(Percentage ~ as.numeric(Year), DAT);
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,","~~italic(r)^2~"="~p,
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3,
                        p = format(summary(m)$p.value, digits = 3)))
  as.character(as.expression(eq));                 
}
eq <- ddply(DAT,.(Class),lm_eqn)

But this one gives an error...and I don't understand :/ Is there a way to also add the F value?

Thank you in advance.

Suusie
  • 149
  • 9
  • What's the specific error you get? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Apr 03 '23 at 13:48
  • You don't need semicolon (;) at the end of a line in R – Julien Apr 03 '23 at 15:48
  • I get the: Error: unexpected symbol in: " p = format(summary(m)$p.value, digits = 3))) as.character" – Suusie Apr 04 '23 at 06:57

0 Answers0