Skip to content
Snippets Groups Projects
Commit fcc84468 authored by mgy20's avatar mgy20
Browse files

Ajustado teste F

parent f0686af4
Branches
No related tags found
No related merge requests found
Showing
with 2170 additions and 4254 deletions
%% Cell type:markdown id:b977bf15-ee73-4730-8651-f8a3caffb393 tags:
# Analise estatistica com Teste CohenD
Teste de effect size
%% Cell type:markdown id:36e8ccea-ef95-4da0-bca9-45bd2fbe5530 tags:
## Instalação dos pacotes
Para fazer o jupyter-lab processar R após a instaçação, dentro do ambiente virtual do python e dentro seu client com sudo `sudo R`, instale os pacotes com o comando a seguir.
%% Cell type:code id:57a4780e-976b-4832-b04a-4680e62ccb5a tags:
``` R
#install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))
#devtools::install_github('IRkernel/IRkernel')
#IRkernel::installspec()
```
%% Cell type:code id:ceab7323-0150-4a5c-8b6f-07464f948b9a tags:
``` R
# Carrega CSV
options(warn = -1)
library(effsize)
df = read.csv("../dados/escola-integers-ks.csv", sep="|")
head(df)
```
%% Output
A data.frame: 6 × 10
| <!--/--> | ANO_CENSO &lt;int&gt; | NUM_COMPUTADOR &lt;int&gt; | NUM_COMPUTADOR_ADM &lt;int&gt; | NUM_COMPUTADPR_ALUNO &lt;int&gt; | NUM_DVD &lt;int&gt; | NUM_FUNCIONARIOS &lt;int&gt; | NUM_SALAS &lt;int&gt; | NUM_SALAS_UTILIZADAS &lt;int&gt; | NUM_SOM &lt;int&gt; | NUM_TV &lt;int&gt; |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2013 | 165 | 35 | 130 | 1 | 42 | 8 | 8 | 1 | 3 |
| 2 | 2013 | 15 | 4 | 11 | 5 | 16 | 5 | 5 | 7 | 6 |
| 3 | 2013 | 25 | 5 | 20 | 2 | 40 | 8 | 8 | 3 | 3 |
| 4 | 2013 | 15 | 8 | 7 | 2 | 45 | 10 | 10 | 2 | 4 |
| 5 | 2013 | 16 | 8 | 8 | 2 | 47 | 12 | 12 | 1 | 6 |
| 6 | 2013 | 10 | 7 | 3 | 1 | 25 | 4 | 4 | 0 | 1 |
A data.frame: 6 × 10
\begin{tabular}{r|llllllllll}
& ANO\_CENSO & NUM\_COMPUTADOR & NUM\_COMPUTADOR\_ADM & NUM\_COMPUTADPR\_ALUNO & NUM\_DVD & NUM\_FUNCIONARIOS & NUM\_SALAS & NUM\_SALAS\_UTILIZADAS & NUM\_SOM & NUM\_TV\\
& <int> & <int> & <int> & <int> & <int> & <int> & <int> & <int> & <int> & <int>\\
\hline
1 & 2013 & 165 & 35 & 130 & 1 & 42 & 8 & 8 & 1 & 3\\
2 & 2013 & 15 & 4 & 11 & 5 & 16 & 5 & 5 & 7 & 6\\
3 & 2013 & 25 & 5 & 20 & 2 & 40 & 8 & 8 & 3 & 3\\
4 & 2013 & 15 & 8 & 7 & 2 & 45 & 10 & 10 & 2 & 4\\
5 & 2013 & 16 & 8 & 8 & 2 & 47 & 12 & 12 & 1 & 6\\
6 & 2013 & 10 & 7 & 3 & 1 & 25 & 4 & 4 & 0 & 1\\
\end{tabular}
%% Cell type:markdown id:2c7eca10-6c0b-427d-be50-c4bf3d224f2d tags:
## Limpeza de Outliers
Para limpar dados que se diferenciam drasticamente do resto do conjunto de dados é utilizado a remoção dos outliers.
%% Cell type:code id:ee18ee6d-1dda-4c65-8347-f3b7e980e83c tags:
``` R
# Function to remove outliers using the IQR method
remove_outliers <- function(data) {
# Calculate the interquartile range (IQR)
Q1 <- quantile(data, 0.25)
Q3 <- quantile(data, 0.75)
IQR <- Q3 - Q1
# Define the lower and upper bounds
lower_bound <- Q1 - 1.5 * IQR
upper_bound <- Q3 + 1.5 * IQR
# Remove outliers
cleaned_data <- data[data >= lower_bound & data <= upper_bound]
# Return the cleaned data
return(cleaned_data)
}
```
%% Cell type:markdown id:7ca6c624-62c0-4fce-b3cd-c47d20c0b85e tags:
## Plot de Grafico
Funções para plot de gráficos
%% Cell type:code id:4c578156-afa1-4432-acb3-d6a143c0b349 tags:
``` R
plot_ecdf <- function(x, x2){
plot(ecdf(x),
xlim = range(c(x, x2)),
col = "blue")
plot(ecdf(x2),
add = TRUE,
lty = "dashed",
col = "red")
}
```
%% Cell type:markdown id:69c80406-3d20-408d-8fce-e4a7aea65dd4 tags:
## Teste Cohen D
Utilizando testes de effect size é possivel determinar a magnitude da relação entre duas amostras
Primeiro é selecionado duas amostras que foram agrupadas pelos anos do censo.
Em seguida é feito uma limpeza dos dados fazendo a retirada dos nulos, outliers e os ordenando.
Finalmente é executado o teste.
%% Cell type:code id:a9629868-ef70-4073-856b-fa3cee413898 tags:
``` R
# Dados agrupados por ano
ANO <- df["ANO_CENSO"]
# Amostras
X <- df["NUM_SALAS"]
Xi <- X[ANO == 2013]
Y <- df["NUM_SALAS"]
Yi <- Y[ANO == 2014]
# Limpeza dos dados
data1 <- sort(remove_outliers(na.omit(Xi)))
data2 <- sort(remove_outliers(na.omit(Yi)))
# Executa teste Cohen D
#print(cohen.d(data1, data2))
result = cohen.d(data1, data2)
print(result$estimate)
# Plotagem dos dados
plot_ecdf(data1, data2)
```
%% Output
[1] -0.03455845
%% Cell type:markdown id:b6c8c14d-5101-4201-b172-28e5b17016df tags:
## Teste CohenD - Anos subsequentes
Execução do teste T entre uma coluna de um ano contra todas as colunas dos anos subsequentes para todos os anos do csv.
%% Cell type:code id:9aa86d14-0e60-4945-9d5f-d058b3c45654 tags:
``` R
# Colunas do csv de saida
colunas = c("coluna1", "ano_coluna1", "coluna2", "ano_coluna2", "tamanho_amostra1", "estatistica_cohend")
output_df = data.frame(matrix(ncol = length(colunas), nrow = 0));
# Remove ANO_CENSO das iteracoes
atributos = names(df)
atributos = atributos[atributos != "ANO_CENSO"]
# Separa os anos em amostras
ANO <- df["ANO_CENSO"]
for(ano in sort(unique(df$ANO_CENSO))){
for(col1 in atributos) {
for(col2 in atributos) {
# Amostra de um ano
X <- df[col1]
Xi <- X[ANO == ano]
# Amostra do ano seguinte
Y <- df[col2]
Yi <- Y[ANO == ano+1]
# Remove NaN, outliers e ordena
data1 <- sort(remove_outliers(na.omit(Xi)))
data2 <- sort(remove_outliers(na.omit(Yi)))
# Pula casos em que não há dados nas amostras
if(length(data1) == 0 || length(data2) == 0){
next
}
# Teste Cohen D
resultado = cohen.d(data1, data2)
# Concatena resultados no dataframe
nova_linha = c(col1, ano, col2, ano+1, length(data1), resultado$estimate)
output_df = rbind(output_df, nova_linha)
}
}
}
output_csv = "Result_COHEND/COHEND_subsequente.csv"
colnames(output_df) <- colunas
write.csv(output_df, file = output_csv, row.names = FALSE)
```
%% Cell type:markdown id:d52a6568-693a-4d59-b050-2f8d6899433e tags:
## Teste CohenD - Anos acumulados
O mesmo teste anterior, porém com o acumulo da amostra conforme o passar dos anos
%% Cell type:code id:bd76fa5b-a42c-434a-b3df-83f12f80a262 tags:
``` R
# Colunas do csv de saida
colunas = c("coluna1", "ano_coluna1", "coluna2", "ano_coluna2", "tamanho_amostra1", "estatistica_cohend")
output_df = data.frame(matrix(ncol = length(colunas), nrow = 0))
ano_start = min(df$ANO_CENSO)
# Remove ANO_CENSO das iteracoes
atributos = names(df)
atributos = atributos[atributos != "ANO_CENSO"]
# Separa os anos em amostras
ANO_COLUMN <- df["ANO_CENSO"]
for(ano in sort(unique(df$ANO_CENSO))){
for(col1 in atributos) {
for(col2 in atributos) {
# Amostra acumulada dos anos
X <- df[col1]
Xi <- X[ANO_COLUMN >= ano_start & ANO_COLUMN <= ano]
# Amostra do ano seguinte
Y <- df[col2]
Yi <- Y[ANO_COLUMN == ano+1]
# Remove NaN, outliers e ordena
data1 <- sort(remove_outliers(na.omit(Xi)))
data2 <- sort(remove_outliers(na.omit(Yi)))
# Pula casos em que não há dados nas amostras
if(length(data1) == 0 || length(data2) == 0){
next
}
# Teste Cohend
resultado = cohen.d(data1, data2)
# Concatena resultados no dataframe
nova_linha = c(col1, ano, col2, ano+1, length(data1), resultado$estimate)
output_df = rbind(output_df, nova_linha)
}
}
}
output_csv = "Result_COHEND/COHEND_acumulado.csv"
colnames(output_df) <- colunas
write.csv(output_df, file = output_csv, row.names = FALSE)
```
This diff is collapsed.
This diff is collapsed.
"coluna1","ano_coluna1","coluna2","ano_coluna2","tamanho_amostra1","estatistica_F","p_valor" "coluna1","ano_coluna1","coluna2","ano_coluna2","tamanho_amostra1","estatistica_f","p_valor"
"ANO_CENSO","2007","ANO_CENSO","2008","237387","NaN","NaN" "ANO_CENSO","2007","ANO_CENSO","2008","237387","NaN","NaN"
"ANO_CENSO","2007","NUM_COMPUTADOR","2008","237387","0","0" "ANO_CENSO","2007","NUM_COMPUTADOR","2008","237387","0","0"
"ANO_CENSO","2007","NUM_COMPUTADOR_ADM","2008","237387","0","0" "ANO_CENSO","2007","NUM_COMPUTADOR_ADM","2008","237387","0","0"
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 1,
"id": "ceab7323-0150-4a5c-8b6f-07464f948b9a", "id": "ceab7323-0150-4a5c-8b6f-07464f948b9a",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -122,7 +122,7 @@ ...@@ -122,7 +122,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 2,
"id": "ee18ee6d-1dda-4c65-8347-f3b7e980e83c", "id": "ee18ee6d-1dda-4c65-8347-f3b7e980e83c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -157,7 +157,7 @@ ...@@ -157,7 +157,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 3,
"id": "4c578156-afa1-4432-acb3-d6a143c0b349", "id": "4c578156-afa1-4432-acb3-d6a143c0b349",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -262,7 +262,7 @@ ...@@ -262,7 +262,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 4,
"id": "9aa86d14-0e60-4945-9d5f-d058b3c45654", "id": "9aa86d14-0e60-4945-9d5f-d058b3c45654",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment