library(dplyr)
contr.treatment(6)
##   2 3 4 5 6
## 1 0 0 0 0 0
## 2 1 0 0 0 0
## 3 0 1 0 0 0
## 4 0 0 1 0 0
## 5 0 0 0 1 0
## 6 0 0 0 0 1
contr.treatment(5)
##   2 3 4 5
## 1 0 0 0 0
## 2 1 0 0 0
## 3 0 1 0 0
## 4 0 0 1 0
## 5 0 0 0 1
contr.treatment(4)
##   2 3 4
## 1 0 0 0
## 2 1 0 0
## 3 0 1 0
## 4 0 0 1
contr.treatment(3)
##   2 3
## 1 0 0
## 2 1 0
## 3 0 1
contr.treatment(2)
##   2
## 1 0
## 2 1
hsb2 = read.table('https://stats.idre.ucla.edu/stat/data/hsb2.csv', header=T, sep=",")
#criando a variável fator race.f
hsb2$race.f = factor(hsb2$race, labels=c("Hispanic", "Asian", "African-Am", "Caucasian"))

tapply(hsb2$write, hsb2$race.f, mean)
##   Hispanic      Asian African-Am  Caucasian 
##   46.45833   58.00000   48.20000   54.05517

1. Dummy Coding

contr.treatment(4)
##   2 3 4
## 1 0 0 0
## 2 1 0 0
## 3 0 1 0
## 4 0 0 1
contrasts(hsb2$race.f) = contr.treatment(4)
summary(lm(write ~ race.f, hsb2))
## 
## Call:
## lm(formula = write ~ race.f, data = hsb2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -23.0552  -5.4583   0.9724   7.0000  18.8000 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   46.458      1.842  25.218  < 2e-16 ***
## race.f2       11.542      3.286   3.512 0.000552 ***
## race.f3        1.742      2.732   0.637 0.524613    
## race.f4        7.597      1.989   3.820 0.000179 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.025 on 196 degrees of freedom
## Multiple R-squared:  0.1071, Adjusted R-squared:  0.0934 
## F-statistic: 7.833 on 3 and 196 DF,  p-value: 5.785e-05

2 Simple Coding

c<-contr.treatment(4)
my.coding<-matrix(rep(1/4, 12), ncol=3)
my.simple<-c-my.coding
my.simple
##       2     3     4
## 1 -0.25 -0.25 -0.25
## 2  0.75 -0.25 -0.25
## 3 -0.25  0.75 -0.25
## 4 -0.25 -0.25  0.75
contrasts(hsb2$race.f)<-my.simple
summary(lm(write~race.f, hsb2))
## 
## Call:
## lm(formula = write ~ race.f, data = hsb2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -23.0552  -5.4583   0.9724   7.0000  18.8000 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  51.6784     0.9821  52.619  < 2e-16 ***
## race.f2      11.5417     3.2861   3.512 0.000552 ***
## race.f3       1.7417     2.7325   0.637 0.524613    
## race.f4       7.5968     1.9889   3.820 0.000179 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.025 on 196 degrees of freedom
## Multiple R-squared:  0.1071, Adjusted R-squared:  0.0934 
## F-statistic: 7.833 on 3 and 196 DF,  p-value: 5.785e-05

3 Deviation Coding

contr.sum(4)
##   [,1] [,2] [,3]
## 1    1    0    0
## 2    0    1    0
## 3    0    0    1
## 4   -1   -1   -1
contrasts(hsb2$race.f) = contr.sum(4)
summary(lm(write ~ race.f, hsb2))
## 
## Call:
## lm(formula = write ~ race.f, data = hsb2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -23.0552  -5.4583   0.9724   7.0000  18.8000 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  51.6784     0.9821  52.619  < 2e-16 ***
## race.f1      -5.2200     1.6314  -3.200  0.00160 ** 
## race.f2       6.3216     2.1603   2.926  0.00384 ** 
## race.f3      -3.4784     1.7323  -2.008  0.04602 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.025 on 196 degrees of freedom
## Multiple R-squared:  0.1071, Adjusted R-squared:  0.0934 
## F-statistic: 7.833 on 3 and 196 DF,  p-value: 5.785e-05

4 Orthogonal Polynomial Coding

hsb2$readcat<-cut(hsb2$read, 4, ordered = TRUE)
table(hsb2$readcat)
## 
## (28,40] (40,52] (52,64] (64,76] 
##      22      93      55      30
tapply(hsb2$write, hsb2$readcat, mean)
##  (28,40]  (40,52]  (52,64]  (64,76] 
## 42.77273 49.97849 56.56364 61.83333
contr.poly(4)
##              .L   .Q         .C
## [1,] -0.6708204  0.5 -0.2236068
## [2,] -0.2236068 -0.5  0.6708204
## [3,]  0.2236068 -0.5 -0.6708204
## [4,]  0.6708204  0.5  0.2236068
contrasts(hsb2$readcat) = contr.poly(4)
summary(lm(write ~ readcat, hsb2))
## 
## Call:
## lm(formula = write ~ readcat, data = hsb2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -18.979  -5.824   1.227   5.436  17.021 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  52.7870     0.6339  83.268   <2e-16 ***
## readcat.L    14.2587     1.4841   9.607   <2e-16 ***
## readcat.Q    -0.9680     1.2679  -0.764    0.446    
## readcat.C    -0.1554     1.0062  -0.154    0.877    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.726 on 196 degrees of freedom
## Multiple R-squared:  0.3456, Adjusted R-squared:  0.3356 
## F-statistic: 34.51 on 3 and 196 DF,  p-value: < 2.2e-16

Testes

library(xlsx)
df = read.xlsx2(file = "dissertação-atualizada.xlsx", sheetIndex = 1, header = T)
head(df)
glimpse(df)
## Rows: 150
## Columns: 39
## $ X.                       <chr> "RT37", "RT65", "N3", "AA18", "AA13", "RT10",…
## $ IDreesMON                <chr> "5", "2.4742268041237114", "3.175257731958763…
## $ BreesMON                 <chr> "2.3649289099526065", "5", "2.156398104265403…
## $ CreesMON                 <chr> "5", "3.2162162162162162", "4.372972972972972…
## $ RedeMon                  <chr> "1171.2261564372725", "610.0863750348286", "4…
## $ IDreesANT                <chr> "5", "2.7874999999999996", "3.637499999999999…
## $ BreesANT                 <chr> "2.7499999999999996", "5", "2.359375", "2.906…
## $ CreesANT                 <chr> "5", "3.8826446280991735", "4.735537190082645…
## $ RedeAnt                  <chr> "1361.931818181818", "829.7535123966942", "64…
## $ IDreesRES                <chr> "5", "3", "3.4823529411764707", "2.6", "3.164…
## $ BreesRES                 <chr> "1.410596026490066", "5", "2.5231788079470197…
## $ CreesRES                 <chr> "5", "3.9624796084828713", "4.732463295269167…
## $ RedeResp                 <chr> "698.594767664605", "911.3703099510604", "664…
## $ IDreesAPR                <chr> "5", "2.873873873873874", "3.2822822822822824…
## $ BreesAPR                 <chr> "2.035294117647059", "5", "2.1607843137254905…
## $ CreesAPR                 <chr> "5", "3.6380697050938346", "4.67828418230563"…
## $ RedeApr                  <chr> "1007.9752066115705", "801.577099888092", "53…
## $ Resilience.Score.RS      <chr> "4239.727948895265", "3152.787297270675", "23…
## $ Disponibilidade          <chr> "4.2727272727272725", "3.8333333333333335", "…
## $ Confiabilidade           <chr> "4.636363636363637", "4", "4.090909090909091"…
## $ DM                       <chr> "8.000000000000002", "9.999999999999998", "8.…
## $ DF                       <chr> "2.04", "0", "0", "0", "0", "5.2", "5.36", "0…
## $ DT                       <chr> "9.999999999999998", "3.960000000000001", "6"…
## $ DE                       <chr> "2.0000000000000004", "3.04", "0", "3.7600000…
## $ ES                       <chr> "1.4200000000000002", "1.48", "4.000000000000…
## $ NF                       <chr> "1.4200000000000002", "2.96", "4.000000000000…
## $ Carga.de.trabalho.GLOBAL <chr> "7.464000000000001", "6.432", "6.600000000000…
## $ Nasc                     <chr> "1979", "1973", "1973", "1986", "1961", "1979…
## $ Sexo                     <chr> "Masculino", "Masculino", "Feminino", "Femini…
## $ Profissão                <chr> "Tecnólogo/a em radiologia", "Técnico/a de ra…
## $ Turno                    <chr> "Manhã", "Tarde", "Manhã", "Manhã", "Manhã", …
## $ DifTur                   <chr> "Sim", "Sim", "Sim", "Sim", "Sim", "Sim", "Si…
## $ ExpLocal                 <chr> "20", "20", "21", "8", "17", "12", "18", "3",…
## $ ExpArea                  <chr> "20", "22", "12", "8", "17", "18", "18", "21"…
## $ Iniciativa               <chr> "5", "5", "5", "3", "5", "5", "5", "5", "5", …
## $ Trajeto                  <chr> "25", "15", "24", "45", "15", "15", "10", "12…
## $ MaisEmp                  <chr> "Sim", "Não", "Não", "Não", "Não", "Sim", "Nã…
## $ Hex                      <chr> "5", "5", "1", "1", "2", "4", "1", "1", "3", …
## $ idade                    <chr> "41", "47", "47", "34", "59", "41", "56", "46…
df$Turno = as.factor(df$Turno)
contrasts(df$Turno)
##       Manhã Noite Tarde
##           0     0     0
## Manhã     1     0     0
## Noite     0     1     0
## Tarde     0     0     1
df$Iniciativa = as.factor(df$Iniciativa)
contrasts(df$Iniciativa)
##   2 3 4 5
##   0 0 0 0
## 2 1 0 0 0
## 3 0 1 0 0
## 4 0 0 1 0
## 5 0 0 0 1
df$Carga.de.trabalho.GLOBAL = as.numeric(as.character(df$Carga.de.trabalho.GLOBAL))
res = lm(df$Carga.de.trabalho.GLOBAL~df$Turno + df$Iniciativa)
summary(res)
## 
## Call:
## lm(formula = df$Carga.de.trabalho.GLOBAL ~ df$Turno + df$Iniciativa)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.0007 -0.9025 -0.0212  0.9079  3.1623 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     5.35149    0.54308   9.854   <2e-16 ***
## df$TurnoNoite   0.11748    0.33903   0.347    0.729    
## df$TurnoTarde   0.21312    0.24576   0.867    0.387    
## df$Iniciativa3  0.09452    0.70600   0.134    0.894    
## df$Iniciativa4  0.36874    0.55136   0.669    0.505    
## df$Iniciativa5  0.54123    0.54424   0.994    0.322    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.36 on 142 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.01595,    Adjusted R-squared:  -0.0187 
## F-statistic: 0.4603 on 5 and 142 DF,  p-value: 0.8052

Chi-squared test

chisq.test(df$Turno, df$Iniciativa)
## 
##  Pearson's Chi-squared test
## 
## data:  df$Turno and df$Iniciativa
## X-squared = 155.57, df = 12, p-value < 2.2e-16
chisq.test(df$Turno, df$Carga.de.trabalho.GLOBAL)
## 
##  Pearson's Chi-squared test
## 
## data:  df$Turno and df$Carga.de.trabalho.GLOBAL
## X-squared = 249.14, df = 262, p-value = 0.7062
df = df[1:148, ]
QuiQuadrado = data.frame()
for (i in 2:ncol(df)) {
    for (a in 2:ncol(df)) {
      uc = chisq.test(df[,i], df[,a],)
      QuiQuadrado = rbind(QuiQuadrado, c(colnames(df)[i], colnames(df)[a],  uc$p.value))
  }
}
names(QuiQuadrado) = c("var 1", "var2", "Coeficiente")
QuiQuadrado

Contingency coefficient C

library('DescTools')
ContCoef(df$Turno, df$Iniciativa, correct = FALSE)
## [1] NaN
Coeficiente = data.frame()
for (i in 2:ncol(df)) {
    for (a in 2:ncol(df)) {
      uc = ContCoef(df[,i], df[,a], correct = FALSE)
      Coeficiente = rbind(Coeficiente, c(colnames(df)[i], colnames(df)[a],  uc))
  }
}
names(Coeficiente) = c("var 1", "var2", "Coeficiente")
Coeficiente

Cramer’s V

library('rcompanion')
cramerV(df$Turno, df$Iniciativa, bias.correct = FALSE)
## Cramer V 
##   0.1363
Cramer = data.frame()
for (i in 2:ncol(df)) {
    for (a in 2:ncol(df)) {
      uc = cramerV(df[,i], df[,a], bias.correct = FALSE)
      Cramer = rbind(Cramer, c(colnames(df)[i], colnames(df)[a],  uc))
  }
}
names(Cramer) = c("var 1", "var2", "Coeficiente")
Cramer

Uncertainty coefficient

UncertCoef(table(df$Turno, df$Iniciativa), direction = "column")
## [1] 0.02024088
ncol(df)
## [1] 39
incertezas = data.frame()
for (i in 2:ncol(df)) {
    for (a in 2:ncol(df)) {
      uc = UncertCoef(table(df[,i], df[,a]), direction = "column")
      incertezas = rbind(incertezas, c(colnames(df)[i], colnames(df)[a],  uc))
  }
}
names(incertezas) = c("Var explicativa", "Var Explicada", "Coeficiente Incerteza")
incertezas