loess regression on each group with dplyr::group_by()

后端 未结 2 623
小蘑菇
小蘑菇 2020-12-11 05:56

Alright, I\'m waving my white flag.

I\'m trying to compute a loess regression on my dataset.

I want loess to compute a different set of points that plots a

2条回答
  •  感动是毒
    2020-12-11 06:21

    You may have already figured this out -- but if not, here's some help.

    Basically, you need to feed the predict function a data.frame (a vector may work too but I didn't try it) of the values you want to predict at.

    So for your case:

    fems <- fems %>% 
      group_by(CpG) %>% 
      arrange(CpG, AVGMOrder) %>% 
      mutate(Loess = predict(loess(Meth ~ AVGMOrder, span = .5, data=.),
        data.frame(AVGMOrder = seq(min(AVGMOrder), max(AVGMOrder), 1))))
    

    Note, loess requires a minimum number of observations to run (~4? I can't remember precisely). Also, this will take a while to run so test with a slice of your data to make sure it's working properly.

提交回复
热议问题