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

后端 未结 2 616
小蘑菇
小蘑菇 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:18

    This is a neat Tidyverse way to make it work:

    library(dplyr)
    library(tidyr)
    library(purrr)
    library(ggplot2)
    
    models <- fems %>%
            tidyr::nest(-CpG) %>%
            dplyr::mutate(
                    # Perform loess calculation on each CpG group
                    m = purrr::map(data, loess,
                                   formula = Meth ~ AVGMOrder, span = .5),
                    # Retrieve the fitted values from each model
                    fitted = purrr::map(m, `[[`, "fitted")
            )
    
    # Apply fitted y's as a new column
    results <- models %>%
            dplyr::select(-m) %>%
            tidyr::unnest()
    
    # Plot with loess line for each group
    ggplot(results, aes(x = AVGMOrder, y = Meth, group = CpG, colour = CpG)) +
            geom_point() +
            geom_line(aes(y = fitted))
    

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题