Questões

Questão 1

MRT_1F <- c(517.1468515630205, 85.13094142168089, 30.333207896694553,
            12.694776264558937, 3.3041601673945418, 1.1823111717498882,
            1.1892293502386786)
MRT_3F <- c(156.68929936163462, 11.540837783562276, 0.4512835621696538,
            0.4509797929766453, 0.4502068233039181, 0.4496185276300172,
            0.4543157082191288)
MRT_5F <- c(83.90319666471157, 0.3068151086494968, 0.30522314133037304,
            0.3072588968084928, 0.30655265997285697, 0.3055812715727718,
            0.3053297166713006)
MRT_10F <- c(29.55430642951759, 0.19832832665772515, 0.1971923924717474,
             0.19796648905716516, 0.19615594370806338, 0.2034569237883263,
             0.19617420889447737)
MRT_15F <- c(11.317736530583566, 0.167364215666193, 0.16172168266811013,
             0.16701085329580515, 0.1598052657153692, 0.1645934043532696,
             0.16216563797118075)
MRT_sem_F <- c(11.93430909937736, 0.6095414637034009,
               0.6060645101029295, 0.612167181646899, 0.6146761002685637,
               0.6096747087200697, 0.6125810476877268)
clock <- c(0.1, 0.5, 1, 1.5, 2, 2.5, 3)

layout(matrix(c(1, 1,
                2, 3,
                4, 5,
                6, 0), nrow = 4, byrow = TRUE),
       heights = c(1.5, 1, 1, 1))

par(mar = c(5, 5, 3, 2))
plot(clock, MRT_1F, type = "b", pch = 4, ylim = c(0, max(MRT_1F)),
     xlab = "Time between Things requests (seconds)",
     ylab = "Response Time (sec)",
     main = "Response Time by Workload")
lines(clock, MRT_3F, type = "b", pch = 3, col = "red")
lines(clock, MRT_5F, type = "b", pch = 2, col = "orange")
lines(clock, MRT_10F, type = "b", pch = 6, col = "blue")
lines(clock, MRT_15F, type = "b", pch = 5, col = "purple")
lines(clock, MRT_sem_F, type = "b", pch = 1, col = "green4")
legend("topright",
       legend = c("1 Fog", "3 Fogs", "5 Fogs", "10 Fogs", "15 Fogs", "wo Fog"),
       col = c("black", "red", "orange", "blue", "purple", "green4"),
       pch = c(4, 3, 2, 6, 5, 1), lty = 1, cex = 0.8)

plot_bar_compara <- function(vetor_fog, nome_fog) {
  dados <- rbind(MRT_sem_F, vetor_fog)
  rownames(dados) <- c("wo Fog", nome_fog)
  par(mar = c(5, 5, 2, 1))
  barplot(dados,
          beside = TRUE,
          log = "y",
          col = c("#E6E6E6", "#666666"),
          names.arg = clock,
          xlab = "Time between Things requests",
          ylab = "Response time (s)",
          legend.text = TRUE,
          args.legend = list(x = "topright", cex = 0.7, bty = "n"))
}

plot_bar_compara(MRT_1F, "1 Fog")
plot_bar_compara(MRT_3F, "3 Fogs")
plot_bar_compara(MRT_5F, "5 Fogs")
plot_bar_compara(MRT_10F, "10 Fogs")
plot_bar_compara(MRT_15F, "15 Fogs")

Questão 2

qualidade_refeicao <- matrix(c(53.8, 33.9, 2.6, 0.0,
                               43.6, 54.2, 60.5, 21.4,
                               2.6, 11.9, 36.8, 78.6),
                             nrow = 3, byrow = TRUE)
rownames(qualidade_refeicao) <- c("Good", "Very Good", "Excellent")
colnames(qualidade_refeicao) <- c("$10-19", "$20-29", "$30-39", "$40-49")

barplot(qualidade_refeicao,
        beside = FALSE,
        col = c("lightblue", "lightgreen", "orange"),
        main = "Qualidade da refeição por categoria de preço",
        xlab = "Categoria de preço",
        ylab = "Percentual (%)",
        ylim = c(0, 120),
        legend.text = rownames(qualidade_refeicao),
        args.legend = list(x = "topright", bty = "n"))

Questão 3

dados_maio <- subset(airquality, Month == 5)
temp_celsius <- (dados_maio$Temp - 32) / 1.8

hist(temp_celsius,
     probability = TRUE,
     col = "lightblue",
     border = "white",
     main = "Histograma das temperaturas de maio em Celsius",
     xlab = "Temperatura (°C)",
     ylab = "Densidade")
lines(density(temp_celsius, na.rm = TRUE), col = "red", lwd = 2)

Questão 4

sales <- read.table("https://training-course-material.com/images/8/8f/Sales.txt",
                    header = TRUE)

porcentagem <- round(100 * sales$SALES / sum(sales$SALES), 1)
rotulos <- paste0(porcentagem, "%")
cores <- rainbow(nrow(sales))

pie(sales$SALES,
    labels = rotulos,
    col = cores,
    main = "Percentual de vendas por país")

legend("topright",
       legend = sales$COUNTRY,
       fill = cores,
       cex = 0.8,
       bty = "n")

Questão 5

boxplot(count ~ spray,
        data = InsectSprays,
        outline = FALSE,
        col = "yellow",
        main = "Contagem de insetos por tipo de inseticida",
        xlab = "Tipo de inseticida",
        ylab = "Contagem de insetos")

Questão 6

converter_para_mb <- function(x) {
  numero <- as.numeric(gsub("[^0-9.]", "", x))
  unidade <- toupper(gsub("[0-9. ]", "", x))

  ifelse(unidade == "TB", numero * 1000000,
         ifelse(unidade == "GB", numero * 1024,
                ifelse(unidade == "MB", numero,
                       ifelse(unidade == "KB", numero / 1024, numero))))
}

preparar_monitoramento <- function(arquivo) {
  dados <- read.csv(arquivo, stringsAsFactors = FALSE)
  dados$currentTime <- as.POSIXct(dados$currentTime,
                                  format = "%Y-%m-%d %H:%M:%OS")
  dados$tempo_horas <- as.numeric(difftime(dados$currentTime,
                                           dados$currentTime[1],
                                           units = "hours"))
  dados$usedMemory_MB <- converter_para_mb(dados$usedMemory)
  dados
}

dados_none <- preparar_monitoramento("monitoringCloudData_NONE.csv")
dados_01 <- preparar_monitoramento("monitoringCloudData_0.1.csv")
dados_05 <- preparar_monitoramento("monitoringCloudData_0.5.csv")
dados_1 <- preparar_monitoramento("monitoringCloudData_1.csv")

layout(matrix(1:4, nrow = 2, byrow = TRUE))
par(mar = c(5, 5, 4, 2))

plot(dados_none$tempo_horas, dados_none$usedMemory_MB,
     type = "l",
     main = "Memory Analysis (None Workload)",
     xlab = "Time (hour)",
     ylab = "Used Memory (MB)")

plot(dados_01$tempo_horas, dados_01$usedMemory_MB,
     type = "l",
     main = "Memory Analysis (Workload of 0.1)",
     xlab = "Time (hour)",
     ylab = "Used Memory (MB)")

plot(dados_05$tempo_horas, dados_05$usedMemory_MB,
     type = "l",
     main = "Memory Analysis (Workload of 0.5)",
     xlab = "Time (hour)",
     ylab = "Used Memory (MB)")

plot(dados_1$tempo_horas, dados_1$usedMemory_MB,
     type = "l",
     main = "Memory Analysis (Workload of 1.0)",
     xlab = "Time (hour)",
     ylab = "Used Memory (MB)")

Questão 7

netflix <- read.csv("netflix_titles.csv", stringsAsFactors = FALSE)

netflix_um_pais <- netflix %>%
  filter(!is.na(country), country != "", !grepl(",", country))

top10_paises <- netflix_um_pais %>%
  count(country, sort = TRUE) %>%
  slice_head(n = 10) %>%
  rename(Pais = country, Total = n)

plot_ly(top10_paises,
        labels = ~Pais,
        values = ~Total,
        type = "pie",
        textinfo = "label+percent") %>%
  layout(title = "Top 10 países com mais conteúdos na Netflix")

Questão 8

plot_ly(
  type = "table",
  header = list(
    values = c("País", "Total de conteúdos"),
    fill = list(color = "gray"),
    font = list(color = "white"),
    align = "center"
  ),
  cells = list(
    values = list(top10_paises$Pais, top10_paises$Total),
    align = "center"
  )
) %>%
  layout(title = "Tabela dos 10 países com mais conteúdos")

Questão 9

netflix_decadas <- netflix %>%
  mutate(decada = floor(release_year / 10) * 10) %>%
  count(decada, type)

series_decada <- netflix_decadas %>% filter(type == "TV Show")
filmes_decada <- netflix_decadas %>% filter(type == "Movie")

plot_ly() %>%
  add_trace(data = series_decada,
            x = ~decada,
            y = ~n,
            type = "scatter",
            mode = "lines+markers",
            name = "TV Series",
            line = list(color = "blue")) %>%
  add_trace(data = filmes_decada,
            x = ~decada,
            y = ~n,
            type = "scatter",
            mode = "lines+markers",
            name = "Movies",
            line = list(color = "yellow")) %>%
  layout(title = "Quantidade de conteúdos por década na Netflix",
         xaxis = list(title = "Década"),
         yaxis = list(title = "Qtd. Conteúdo"))

Questão 10

generos_interesse <- c("Dramas", "Action & Adventure", "Comedies")

filmes_genero <- netflix %>%
  filter(type == "Movie",
         release_year >= 2000,
         release_year <= 2010) %>%
  mutate(primeiro_genero = trimws(sub(",.*", "", listed_in))) %>%
  filter(primeiro_genero %in% generos_interesse) %>%
  count(release_year, primeiro_genero)

# Garante que anos/gêneros sem ocorrência apareçam como zero.
grades <- expand.grid(release_year = 2000:2010,
                      primeiro_genero = generos_interesse,
                      stringsAsFactors = FALSE)
filmes_genero <- merge(grades, filmes_genero,
                       by = c("release_year", "primeiro_genero"),
                       all.x = TRUE)
filmes_genero$n[is.na(filmes_genero$n)] <- 0

plot_ly(filmes_genero,
        x = ~release_year,
        y = ~n,
        color = ~primeiro_genero,
        type = "bar") %>%
  layout(title = "Filmes por gênero entre 2000 e 2010",
         xaxis = list(title = "Ano de lançamento"),
         yaxis = list(title = "Qtd. de lançamentos"),
         barmode = "group",
         legend = list(title = list(text = "Gênero")))