library(readxl)
library(tidyverse)
library(plotly)
library(psych)
library(stargazer)
library(reshape2)
library(corrplot)

data <- read_excel("F:/Vida acadêmica/Disciplinas/Metodologia de Pesquisa Experimental/Marca Rugby/Dados/finais/string_excel.xlsx")
attach(data)

# Recoding variáveis invertidas

data$D2 <- 5 - D2 + 1
data$D5 <- 5 - D5 + 1
data$D6 <- 5 - D6 + 1
data$I2 <- 5 - I2 + 1
data$I4 <- 5 - I4 + 1
data$I7 <- 5 - I7 + 1
data$R3 <- 5 - R3 + 1
data$R6 <- 5 - R6 + 1
data$S4 <- 5 - S4 + 1
data$S6 <- 5 - S6 + 1

data$missing <- rowSums(data[,c(4:38)])

data <- data %>% filter(missing > 1) # 27 casos tiveram que ser removidos da amostra

### Descrição da amostra

# Gênero

data$Genero[is.na(data$Genero)] <- 'Não informado'

data$Genero <- factor(data$Genero)

tab <- table(data$Genero)
plot_ly(data, 
        values = tab, 
        type = 'pie', textposition = "inside",
        textinfo = 'percent+value', 
        labels = c("Masculino", "Feminino", "Não Informado")) %>% layout(title="Gênero")
# Tempo de prática
describe(Tempo_Pratica)
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 79 7.15 3.71      6    6.92 2.97   1  16    15 0.56    -0.57 0.42
data %>% ggplot(aes(y = Tempo_Pratica, fill = 'red')) +
  geom_boxplot() +
  labs(y = 'Tempo de Prática do Rugby', x = '') +
  scale_x_discrete(labels = NULL, breaks = NULL) + labs(x = "") +
  guides(fill = F) +
  theme_bw()

# Idade
describe(Idade)
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 79 28.8 6.51     29   28.71 5.93  17  44    27 0.04    -0.52 0.73
data %>% ggplot(aes(y = Idade, fill = 'red')) +
  geom_boxplot() +
  labs(y = 'Tempo de Prática do Rugby', x = '') +
  scale_x_discrete(labels = NULL, breaks = NULL) + labs(x = "") +
  guides(fill = F) +
  theme_bw()

# Remuneração
tab2 <- table(Remuneracao)

plot_ly(data, 
        values = tab2, 
        type = 'pie', textposition = "inside",
        textinfo = 'percent+value', 
        labels = c("Não", "Sim")) %>% layout(title="Remuneração do Rugby")
# Nível de relação com o esporte
tab3 <- table(Nivel_Relacao)

plot_ly(data, 
        values = tab3, 
        type = 'pie', textposition = "inside",
        textinfo = 'percent+value', 
        labels = c("Amador", "Entusiasta", 'Outro', 'Profissional')) %>% layout(title="Nível de relação com o Rugby")
# Paixão

Paixao <- as.data.frame(data[,c(4:10)])
stargazer(Paixao, summary = T, type = 'text', title = "Estatísticas descritivas Paixão")
## 
## Estatísticas descritivas Paixão
## =====================================================
## Statistic N  Mean  St. Dev. Min Pctl(25) Pctl(75) Max
## -----------------------------------------------------
## P1        82 4.829  0.410    3     5        5      5 
## P2        82 4.854  0.389    3     5        5      5 
## P3        82 4.854  0.389    3     5        5      5 
## P4        82 4.598  0.563    3     4        5      5 
## P5        82 4.598  0.541    3     4        5      5 
## P6        82 4.134  0.828    2     4        5      5 
## P7        82 3.768  0.972    1     3        5      5 
## -----------------------------------------------------
Paixao$Id <- seq(1:82)
Paixao_long <- melt(Paixao, id.vars = c("Id"))
colnames(Paixao_long) <- c("id", "Item", "Response")

ggplot(Paixao_long, aes(x = Response, fill = Item)) +
  geom_histogram(bins = 10, show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

ggplot(Paixao_long, aes(x = Response, fill = Item))+
  geom_density(show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

# Integridade

Integridade <- as.data.frame(data[,c(11:17)])
stargazer(Integridade, summary = T, type = 'text', title = "Estatísticas descritivas Integridade")
## 
## Estatísticas descritivas Integridade
## =====================================================
## Statistic N  Mean  St. Dev. Min Pctl(25) Pctl(75) Max
## -----------------------------------------------------
## I1        82 4.451  0.705    2     4        5      5 
## I2        82 4.122  0.807    2     4        5      5 
## I3        82 4.646  0.553    3     4        5      5 
## I4        82 4.512  0.835    1     4        5      5 
## I5        82 4.073  0.681    2     4       4.8     5 
## I6        82 4.390  0.797    1     4        5      5 
## I7        82 4.207  0.842    1     4        5      5 
## -----------------------------------------------------
Integridade$Id <- seq(1:82)
Integridade_long <- melt(Integridade, id.vars = c("Id"))
colnames(Integridade_long) <- c("id", "Item", "Response")

ggplot(Integridade_long, aes(x = Response, fill = Item)) +
  geom_histogram(bins = 10, show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

ggplot(Integridade_long, aes(x = Response, fill = Item))+
  geom_density(show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

# Respeito

Respeito <- as.data.frame(data[,c(18:24)])
stargazer(Respeito, summary = T, type = 'text', title = "Estatísticas descritivas Respeito")
## 
## Estatísticas descritivas Respeito
## =====================================================
## Statistic N  Mean  St. Dev. Min Pctl(25) Pctl(75) Max
## -----------------------------------------------------
## R1        82 4.451  0.612    3     4        5      5 
## R2        82 4.293  0.676    2     4        5      5 
## R3        82 3.500  0.997    1     3        4      5 
## R4        82 4.549  0.570    3     4        5      5 
## R5        82 4.256  0.814    2     4        5      5 
## R6        82 4.171  1.040    1     4        5      5 
## R7        82 4.476  0.633    3     4        5      5 
## -----------------------------------------------------
Respeito$Id <- seq(1:82)
Respeito_long <- melt(Respeito, id.vars = c("Id"))
colnames(Respeito_long) <- c("id", "Item", "Response")

ggplot(Respeito_long, aes(x = Response, fill = Item)) +
  geom_histogram(bins = 10, show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

ggplot(Respeito_long, aes(x = Response, fill = Item))+
  geom_density(show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

# Disciplina

Disciplina <- as.data.frame(data[,c(25:31)])
stargazer(Disciplina, summary = T, type = 'text', title = "Estatísticas descritivas Disciplina")
## 
## Estatísticas descritivas Disciplina
## =====================================================
## Statistic N  Mean  St. Dev. Min Pctl(25) Pctl(75) Max
## -----------------------------------------------------
## D1        82 3.537  0.892    1     3        4      5 
## D2        82 3.293  0.962    1     3        4      5 
## D3        82 3.476  0.984    1     3        4      5 
## D4        82 3.695  0.990    1     3        4      5 
## D5        82 3.976  0.816    2     4       4.8     5 
## D6        82 4.195  0.744    1     4        5      5 
## D7        82 3.598  0.914    1     3        4      5 
## -----------------------------------------------------
Disciplina$Id <- seq(1:82)
Disciplina_long <- melt(Disciplina, id.vars = c("Id"))
colnames(Disciplina_long) <- c("id", "Item", "Response")

ggplot(Disciplina_long, aes(x = Response, fill = Item)) +
  geom_histogram(bins = 10, show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

ggplot(Disciplina_long, aes(x = Response, fill = Item))+
  geom_density(show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

# Solidariedade

Solidariedade <- as.data.frame(data[,c(32:38)])
stargazer(Solidariedade, summary = T, type = 'text', title = "Estatísticas descritivas Disciplina")
## 
## Estatísticas descritivas Disciplina
## =====================================================
## Statistic N  Mean  St. Dev. Min Pctl(25) Pctl(75) Max
## -----------------------------------------------------
## S1        82 3.793  0.813    1     3        4      5 
## S2        82 4.268  0.668    2     4        5      5 
## S3        82 4.268  0.721    1     4        5      5 
## S4        82 4.085  1.102    1     4        5      5 
## S5        82 4.061  0.791    2     4        5      5 
## S6        82 4.329  0.817    1     4        5      5 
## S7        82 4.451  0.669    2     4        5      5 
## -----------------------------------------------------
Solidariedade$Id <- seq(1:82)
Solidariedade_long <- melt(Solidariedade, id.vars = c("Id"))
colnames(Solidariedade_long) <- c("id", "Item", "Response")

ggplot(Solidariedade_long, aes(x = Response, fill = Item)) +
  geom_histogram(bins = 10, show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

ggplot(Solidariedade_long, aes(x = Response, fill = Item))+
  geom_density(show.legend = F)+
  facet_wrap(~Item)+
  theme_bw()

# Recodificação do one-hot-encoding

data$Forte[is.na(data$Forte)] <- 0
data$Fraca[is.na(data$Fraca)] <- 0
data$Controle[is.na(data$Controle)] <- 0
grupo <- as.data.frame(as.numeric(data$Forte))
grupo$Fraca <- as.numeric(data$Fraca)
grupo$Controle <- as.numeric(data$Controle)
colnames(grupo) <- c('Forte', 'Fraca', 'Controle')
inds <- which(rowSums(grupo) == 1)
grupo$grupo <- toupper(names(grupo)[max.col(grupo)])
data <- cbind(data, grupo$grupo)
colnames(data)[50] <- 'Condição'

## Correlações

CorMat <- cor(as.matrix(data[,c(4:38)]))
corrplot(CorMat,order="hclust",type="upper",method="ellipse",
         tl.pos = "lt", tl.cex = 0.8)

corrplot(CorMat,
         order="original",
         type="lower",
         method="shade",
         diag = FALSE,
         tl.cex = 0.8, 
         title = 'Matriz de Correlação',
         tl.col = "black",
         tl.offset = 0.5,
         mar = c(0, 0, 2, 0))