Multiple ablations in xyplot

I have a "long" data frame that is defined as:

q <- data.frame(Indicator.Code=factor(),Year=numeric(),Value=numeric()) 

and try to build in one xyplot values โ€‹โ€‹as a function of the year for each other Indicator.Code , as shown below

 xyplot( Value~Year,data=q,group=Indicator.Code) 

So far so good. Now I am trying to add lines corresponding to linear regressions

 rlm(q$Value[q$Indicator.Code==a]~q$Year[q$Indicator.Code==a]) 

for all Indicator.Code values.

I do not know how to do that. The usual way to add regression lines, i.e.

 xyplot( Value~Year,data=q,group=Indicator.Code), panel = function(x, y) { panel.xyplot(x, y) panel.abline(rlm(y ~ x)) })) 

not working properly (it calculates a single regression and adds a single regression line for the entire dataset). In addition, I have already calculated the regressions (I need them for things other than graphics), and I hate the idea of โ€‹โ€‹reprogramming them.

Any hints a newbie could listen to?

+4
source share
2 answers

I am a ggplot2 addict :). The equivalent in ggplot2 does what you expect:

 library(ggplot2) ggplot(q, aes(x = Year, y = Value, color = Indicator.Code)) + geom_point() + stat_smooth(method = "rlm") 

Please note that I believe that you are passing a function as a method , but without a reproducible example, it is difficult to verify.

+2
source

For a custom panel function that displays individual characters for each group , the lattice requires you to wrap the actual panel function when you call panel.superpose() . Here is an example using the data in mtcars data.frame.

 library(lattice) library(MASS) myPanel <- function(x,y,...) { panel.xyplot(x,y,...) panel.abline(rlm(y~x), ...) } xyplot(mpg~disp, group = cyl, data = mtcars, panel = function(...) panel.superpose(panel.groups = myPanel, ...)) ## Or, equivalently: xyplot(mpg~disp, group = cyl, data = mtcars, panel = panel.superpose, panel.groups = myPanel) 

enter image description here

+2
source

Source: https://habr.com/ru/post/1479007/


All Articles