Skip to main content
2 de 3
correção texto
Marconcilio Souza
  • 12,6mil
  • 10
  • 40
  • 106

Como encaixar uma curva de Lowess em dados longitudinais usando R?

Tenho os seguintes dados:

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))

Tentei de todas as maneiras encaixar uma curva de Lowess, uma técnica de suavização por regressão local, nesta distribuição. Usei o seguinte código:

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')

Mas só consigo o seguinte gráfico:

inserir a descrição da imagem aqui

Estou usando a biblioteca ggplot2, mas não tem problema caso consiga me ajudar a encaixar essa curva usando outra biblioteca.

Cloves Paiva
  • 368
  • 1
  • 4
  • 11