1 Objetivos

  • Construir a tabela de distribuição t de Student usando a linguagem R.
  • Aprender a encontrar o valor t crítico, com base nos graus de liberdade (\(n-1\)) e no nível de confiança (\(\alpha\)) estabelecido.
  • O valor t é exigido nos estimadores de erro de amostragem e intervalo de confiança para a estimativa da média populacional da variável de interesse.

2 Construindo a tabela (Bicaudal)

  • A função qt() pode ser usada para obter os valores críticos da Distribuição t-Student. Os principais argumentos da função são: p = vetor de probabilidades; e df = degrees of freedom (graus de liberdade).
## Função para gerar os valores da distribuição t de Student (Bicaudal)

library(dplyr)

table_t <- function(cl, df){
  mat <- matrix(data = NA, 
                nrow = length(df), 
                ncol = length(cl),
                dimnames = list(df, cl)
                )
  
  for(i in seq_along(df)) {
    for(j in seq_along(cl)) {
      mat[i,j] = abs(qt(p = cl[j]/2, df = df[i], lower.tail = F))
    }
  }
  return(mat)
}
## Cria vetores com valores para nível de confiança e graus de liberdade

cl <- c(0.1, 0.05, 0.025, 0.01, 0.005, 0.001, 0.0005)
df <- c(1:30, seq(from = 40, to = 120, by = 10))
## Imprime a tabela da distribuição t-Student

table_t(cl=cl, df=df)
         0.1      0.05     0.025      0.01      0.005      0.001       5e-04
1   6.313752 12.706205 25.451700 63.656741 127.321336 636.619249 1273.239283
2   2.919986  4.302653  6.205347  9.924843  14.089047  31.599055   44.704587
3   2.353363  3.182446  4.176535  5.840909   7.453319  12.923979   16.326335
4   2.131847  2.776445  3.495406  4.604095   5.597568   8.610302   10.306255
5   2.015048  2.570582  3.163381  4.032143   4.773341   6.868827    7.975653
6   1.943180  2.446912  2.968687  3.707428   4.316827   5.958816    6.788340
7   1.894579  2.364624  2.841244  3.499483   4.029337   5.407883    6.081756
8   1.859548  2.306004  2.751524  3.355387   3.832519   5.041305    5.617411
9   1.833113  2.262157  2.685011  3.249836   3.689662   4.780913    5.290654
10  1.812461  2.228139  2.633767  3.169273   3.581406   4.586894    5.048973
11  1.795885  2.200985  2.593093  3.105807   3.496614   4.436979    4.863333
12  1.782288  2.178813  2.560033  3.054540   3.428444   4.317791    4.716459
13  1.770933  2.160369  2.532638  3.012276   3.372468   4.220832    4.597461
14  1.761310  2.144787  2.509569  2.976843   3.325696   4.140454    4.499155
15  1.753050  2.131450  2.489880  2.946713   3.286039   4.072765    4.416613
16  1.745884  2.119905  2.472878  2.920782   3.251993   4.014996    4.346349
17  1.739607  2.109816  2.458051  2.898231   3.222450   3.965126    4.285828
18  1.734064  2.100922  2.445006  2.878440   3.196574   3.921646    4.233167
19  1.729133  2.093024  2.433440  2.860935   3.173725   3.883406    4.186935
20  1.724718  2.085963  2.423117  2.845340   3.153401   3.849516    4.146028
21  1.720743  2.079614  2.413845  2.831360   3.135206   3.819277    4.109579
22  1.717144  2.073873  2.405473  2.818756   3.118824   3.792131    4.076900
23  1.713872  2.068658  2.397875  2.807336   3.103997   3.767627    4.047437
24  1.710882  2.063899  2.390949  2.796940   3.090514   3.745399    4.020739
25  1.708141  2.059539  2.384610  2.787436   3.078199   3.725144    3.996435
26  1.705618  2.055529  2.378786  2.778715   3.066909   3.706612    3.974219
27  1.703288  2.051831  2.373417  2.770683   3.056520   3.689592    3.953832
28  1.701131  2.048407  2.368452  2.763262   3.046929   3.673906    3.935058
29  1.699127  2.045230  2.363846  2.756386   3.038047   3.659405    3.917714
30  1.697261  2.042272  2.359562  2.749996   3.029798   3.645959    3.901643
40  1.683851  2.021075  2.328935  2.704459   2.971171   3.550966    3.788405
50  1.675905  2.008559  2.310914  2.677793   2.936964   3.496013    3.723141
60  1.670649  2.000298  2.299046  2.660283   2.914553   3.460200    3.680708
70  1.666914  1.994437  2.290639  2.647905   2.898734   3.435015    3.650913
80  1.664125  1.990063  2.284372  2.638691   2.886972   3.416337    3.628844
90  1.661961  1.986675  2.279520  2.631565   2.877884   3.401935    3.611841
100 1.660234  1.983972  2.275652  2.625891   2.870652   3.390491    3.598339
110 1.658824  1.981765  2.272497  2.621265   2.864759   3.381179    3.587359
120 1.657651  1.979930  2.269875  2.617421   2.859865   3.373454    3.578254

Produzindo uma tabela mais elegante…

library(DT)

# Custom container
sketch <- htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(rowspan = 2, 'gl'),
      th(class = 'dt-center', colspan = 7, 'α')
    ),
    tr(
      lapply(cl, th)
    )
  )
))


datatable(table_t(cl=cl, df=df) %>% 
            as_tibble(rownames = NA) %>% 
            tibble::rownames_to_column("gl"),
          container = sketch,
          rownames = F,
          style = "default",
          class = "display",
          extensions=c("Buttons",'ColReorder'),
          options = list(
            colReorder = TRUE,
            pageLength = 39,
            dom = 'Bfrtip',
            buttons = c('copy', 'csv', 'excel', 'pdf', 'print', I('colvis')),
            scroller = TRUE,
            lengthChange = FALSE,
            searchHighlight = TRUE,
            initComplete = JS(
              "function(settings, json) {",
              "$(this.api().table().header()).css({'background-color': '#517fb9', 'color': '#fff'});","}"
              )
            ),
          caption = htmltools::tags$caption(
            style = 'caption-side: bottom; text-align: center;',
            'Table: ', htmltools::em('Distribuição t-Student (Bicaudal)')
            )
          ) %>% 
  formatRound(columns = -1, digits = 4) %>% 
  formatStyle(columns = c("gl"), fontWeight = 'bold', `text-align` = 'left')

3 Visualizando a distribuição t de Student

# Gera uma sequência de 100 valores entre -4 e 4
x <- seq(-4, 4, length = 100)

# Define vetor de valores para graus de liberdade
df <- c(1, 4, 10, 20)

# Define cores para as linhas
colour <- c("red", "blue", "darkgreen", "gold", "black")

# Plota a distribuição Normal (Gaussiana)
plot(x, dnorm(x), type = "l", lty = 2, 
     xlab = "Valor t", ylab = "Densidade",
     main = "Comparação de distribuições t", 
     col = "black")

# Adiciona as linhas para as distribuições t, com diferentes graus de liberdade
for (i in 1:4){
  lines(x, dt(x, df[i]), col = colour[i])
  }

# Adiciona legenda
labels <- c("df = 1", "df = 4", "df = 10", "df = 20", "Normal")

legend("topright", labels, col = colour,
       title = "Distribuições t",
       lty = c(1, 1, 1, 1, 2)
       )