How to fit a Lowess curve into longitudinal data using R?

4

I have the following data:

structure(list(X26 = c(1.69, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA), X27 = c(0.94, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA), X28 = c(1.03, NA, NA, NA, NA, NA, 1.95, 4.83, 
NA, NA, NA, 1.52, 3.44, NA, NA, NA, NA, NA, NA, NA, NA, 1.91, 
NA, NA, NA, NA, NA, NA, NA), X29 = c(1.52, NA, NA, 1.37, NA, 
NA, 2.1, 1.26, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1.77, NA, NA, NA, NA, NA, NA, NA), X30 = c(0.91, 3.38, 1.33, 
1.16, NA, NA, 1.86, 1.48, NA, NA, 1.26, 1.33, 1.44, NA, NA, NA, 
NA, 2.1, 2.92, NA, 2.41, 1.23, NA, 1.26, 2.99, 1.33, NA, NA, 
1.77), X31 = c(0.91, 2.05, 1.6, NA, NA, 2.57, NA, NA, NA, NA, 
NA, 0.91, NA, NA, NA, 2.05, 2.1, 2.05, 1.37, NA, 2.41, 1.26, 
NA, 1.52, NA, 1.03, NA, NA, 2.35), X32 = c(1.6, 3.65, 1.6, 0.86, 
1.6, NA, 2.05, NA, NA, 2.25, 2.8, NA, NA, 2.74, NA, 1.64, 2.41, 
2.8, NA, 1.16, 1.73, 1.12, NA, 1.95, 2.92, 1.6, NA, 1.64, NA), 
    X33 = c(2.74, NA, 2.41, NA, 2.52, 3.11, NA, 3.11, NA, 2.05, 
    2.92, NA, NA, 2.74, 1.6, 1.95, 2.63, NA, 1.33, NA, NA, 1.52, 
    1.77, NA, NA, NA, 2.35, 2.86, 2.35), X34 = c(NA, 4.25, 3.65, 
    1.6, 2.1, 5.18, 2.99, NA, 4.25, 2.05, 4.41, NA, NA, NA, NA, 
    3.18, NA, 4.02, 2.1, 2.35, 2.74, NA, 2.63, 4.33, 5.18, 2.2, 
    2.35, NA, NA), X35 = c(2.57, 4.83, 4.83, NA, 2.63, NA, NA, 
    4.1, 2.92, NA, 5.18, NA, NA, 3.38, 2.2, NA, 4.25, NA, NA, 
    1.91, NA, NA, NA, NA, 5.93, NA, 2.05, 2.41, NA), X36 = c(NA, 
    NA, NA, 2.35, NA, 6.54, 2.41, NA, NA, 4.25, NA, NA, NA, 4.1, 
    2.35, 3.18, NA, NA, 1.95, 2.74, 5.36, NA, 3.65, NA, NA, 2.99, 
    2.57, NA, 2.74), X37 = c(2.74, 6.13, NA, 3.38, NA, NA, NA, 
    NA, 3.44, NA, 8.24, NA, NA, 4.33, NA, 4.1, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA), X38 = c(NA, NA, NA, 
    3.31, NA, NA, 2.99, NA, NA, NA, NA, NA, NA, NA, 5.18, NA, 
    NA, NA, NA, 3.94, NA, NA, NA, NA, NA, NA, 3.38, 2.74, 3.51
    ), X39 = c(NA, NA, NA, NA, NA, NA, NA, NA, 4.25, NA, NA, 
    NA, NA, 4.25, NA, NA, NA, NA, 3.31, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), X40 = c(2.92, NA, NA, 5.09, NA, NA, NA, 
    NA, 7.88, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 4.1, NA)), .Names = c("X26", "X27", 
"X28", "X29", "X30", "X31", "X32", "X33", "X34", "X35", "X36", 
"X37", "X38", "X39", "X40"), class = "data.frame", row.names = c(NA, 
-29L))

I have tried every way to fit a Lowess curve, a local regression smoothing technique, into this distribution. I used the following code:

library(dplyr)

library(tidyr)

library(ggplot2)

dados <-  dput(AIG)

dados %>%

mutate(id = row_number()) %>%

gather(semana, tamanho, -id) %>%

mutate(semana = factor(semana, levels = c("X26", "X27", "X28", "X29", 
"X30", "X31", "X32", "X33", "X34", "X35", "X36", "X37", "X38", "X39", "X40")))%>%

ggplot(aes(x = semana, y = tamanho ,group = id)) + geom_point(na.rm = TRUE) + geom_smooth(method = "loess") + ggtitle('Gráfico do grupo AIG')

But I only get the following graph:

I'm using the ggplot2 library, but no problem if you can help me fit that curve into another library.

    
asked by anonymous 20.06.2016 / 16:31

1 answer

4

Your code has two problems:

  • The loess function only accepts numeric variables, in which case semana is factor . geom_smooth always makes loess(y ~ x) .
  • With group = id ggplot is trying to make a curve per person, which is not what you want.
  • In the following code, I added a part that transforms the variable semana into numeric and I remove the argument group = id .

    library(dplyr) 
    library(tidyr)
    library(stringr)
    library(ggplot2)
    dados %>%
      mutate(id = row_number()) %>%
      gather(semana, tamanho, -id) %>%
      mutate(semana = str_replace_all(semana, "X", "") %>% as.numeric()) %>%
      ggplot(aes(x = semana, y = tamanho)) + 
      geom_point(na.rm = TRUE) + 
      geom_smooth(method = "loess", na.rm = T) + ggtitle('Gráfico do grupo AIG')
    

        
    20.06.2016 / 21:13