Importando algumas funções que criei para facilitar a exploração de dados
source("../Useful functions.R")
library(car)
library(tourr)
library(VIM)
library(Amelia)
iris_mod <- read.csv(file = "iris modificada.csv", sep = ";")
Vamos dar uma checada visual na planilha. A função “View” formata a planilha para um melhor aspecto visual.
A função “head”“ apresenta as primeiras linhas de uma planilha.
View(iris_mod)
head(iris_mod)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5,1 3,5 1,4 0,2 setosa
## 2 4,9 3 1,4 0,2 setosa
## 3 4,7 3,2 1,3 0,2 setosa
## 4 4,6 3,1 1,5 0,2 setosa
## 5 5 3,6 1,4 0,2 setosa
## 6 5,4 3,9 1,7 0,4 setosa
A função abaixo é uma das mais importantes para checagem da correta importação dos dados. Um conhecimento básico dos tipos de dados no R é necessário para interpretar seu resultado.
str(iris_mod)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: Factor w/ 35 levels "4,3","4,4","4,5",..: 9 7 5 4 8 12 4 8 2 7 ...
## $ Sepal.Width : Factor w/ 23 levels "2","2,2","2,3",..: 15 10 12 11 16 19 14 14 9 11 ...
## $ Petal.Length: Factor w/ 43 levels "1","1,1","1,2",..: 5 5 4 6 5 8 5 6 5 6 ...
## $ Petal.Width : Factor w/ 22 levels "0,1","0,2","0,3",..: 2 2 2 2 2 4 3 2 2 1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Há quatro tipos básicos de dados no R:
Nesse caso, é possível observar que as colunas que deveriam ser numéricas estão
como fatores, ou seja, como variáveis nominais.
Em função disso, a tentativa de efetuar qualquer operação matemática ocasionará um erro. Exemplo:
Dica rápida: Para selecionar uma coluna de uma planilha (dataframe) coloque colchetes após o nome da planilha e o número da coluna desejada (precedido por vírgula) dentro do parênteses. Ex.: minha.planilha[ , 1]
sum(iris_mod[, 1])
## Error: sum not meaningful for factors
## No popular, o R quis dizer: Tá de bincadeira, né? Como é que eu somo
## esse trem?
Eu criei algumas funções para facilitar a checagem e exploração de dados. Para separar essas funções de outras contidas no R, elas são sempre terminadas com "Teo”.
Todas essas funções especiais estão contidas no script “Useful functions” carregado no início deste script.
A função a seguir checa se variáveis aparentemente numéricas foram importadas como fatores:
check_false_factor_Teo(iris_mod)
## The following variables seem to be false factors, i.e., numeric
## columns that contain one or more non numeric values:
## Sepal.Length
## Sepal.Width
## Petal.Length
## Petal.Width
## This(These) character(s) may be the cause:
## [1] "," ""
Causa do problema:
R utiliza ponto como default para decimais, não vírgula. Caso queiramos importar o arquivo corretamente, devemos trocar vírgulas por ponto no arquivo original OU apenas informar que estamos usando vírgula para decimais:
iris_mod <- read.csv(file = "Iris modificada.csv", sep = ";", dec = ",")
Vamos checar se a importação foi feita corretamente agora:
str(iris_mod)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Para desencargo de consciência, vamos checar que está tudo ok com as colunas definidas como factor novamente:
check_false_factor_Teo(iris_mod)
## Go on, dude. Everything seems to be fine!
Vamos checar o balanceamento dos dados (número de repetições por classes ou combinação de classes de variáveis categóricas)
table(iris_mod$Species)
##
## setosa versicolor virginica
## 50 50 50
Checar dados faltantes:
find_NAs_Teo(iris)
## Number of missing values per variable:
## Sepal.Length: 0
## Sepal.Width: 0
## Petal.Length: 0
## Petal.Width: 0
## Species: 0
Após checar que a importação foi feita apropriadamente, vamos começar a análise propriamente dita.
Esse comando é desaconselhável para usuários com experiência básica em R. Para quem está começando, entretanto, ele pode ser útil. Após esse comado, eu posso chamar uma coluna da dataframe “anexada” sem precisar indicar a dataframe:
attach(iris_mod)
Sepal.Length
## [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4
## [18] 5.1 5.7 7.0 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5
## [35] 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0
## [52] 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8
## [69] 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4
## [86] 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8
## [103] 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7
## [120] 6.0 5.4 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7
## [137] 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
Boxplots por variável:
layout(matrix(1:4, ncol = 2))
boxplot(Petal.Length, main = "Petal Length")
boxplot(Sepal.Length, main = "Sepal Length")
boxplot(Petal.Width, main = "Petal Width")
boxplot(Sepal.Width, main = "Sepal Width")
Até aqui tudo parece estar ok. Entretanto, uma informação importante, espécies, não está sendo considerada.
Incluindo espécies:
layout(matrix(1:4, ncol = 2))
boxplot(Petal.Length ~ Species, main = "Petal Length")
boxplot(Sepal.Length ~ Species, main = "Sepal Length")
boxplot(Petal.Width ~ Species, main = "Petal Width")
boxplot(Sepal.Width ~ Species, main = "Sepal Width")
Ops! Alguma suspeita?
Um sujeito precipitado sugeriria uma transformação ou a remoção dos outliers mas problemas desse tipo podem (comumente) ocorrer por erros de digitação.
Esse é o momento apropriado para checar os dados originais.
Vamos identificar quem são esses pontos extremos (isso não será visível aqui)
boxplot(Petal.Length ~ Species, main = "Petal Length")
identify(xy.coords(x = Species, y = Petal.Length))
## integer(0)
Ao executar esse comando no R, o usuário deve:
Clicar nos pontos suspeitos e então clique em stop –> stop locator (no canto superior esquerdo do gráfico)
dotchart(Petal.Length)
dotchart(Petal.Length, groups = Species)
sel <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
dotchart_cat_Teo(quant_var = sel, cat_var = "Species", data = iris_mod)
## Loading required package: plyr
## Attaching package: 'plyr'
## The following object(s) are masked from 'package:tourr':
##
## ozone
Os gráficos das linhas abaixo não aparecerão nesse HTML, execute o script fornecido para ver os resultados.
scatter3d(x = Petal.Length, y = Petal.Width, z = Sepal.Length, surface = FALSE)
scatter3d(x = Petal.Length, y = Petal.Width, z = Sepal.Length, group = Species,
surface = FALSE)
identify3d(x = Petal.Length, y = Petal.Width, z = Sepal.Length, group = Species)
scatterplotMatrix(iris[, -5], group = Species, smooth = FALSE, reg.line = FALSE)
Falta de atenção em relação a grupos pode matar a análise. Compare com o gráfico acima.
pair_cor_Teo(iris[, 1:4], group = Species)
pair_cor_Teo(iris[Species == "setosa", 1:4])
pair_cor_Teo(iris[Species == "virginica", 1:4])
pair_cor_Teo(iris[Species == "versicolor", 1:4])
Não será possível visualizar op gráfico aqui. Será necessário executar o script fornecido referente a esse HTML.
animate(data = iris[, 1:4], tour_path = grand_tour(1), display = display_xy(col = iris[,
5]))
render(data = iris[, 1:4], tour_path = grand_tour(), display = display_xy(col = iris[,
5]), dev = "pdf", file = "Projection Pursuit (iris).pdf", frames = 200)
Dados completamente aleatórios podem produzir altas significâncias.
Nas função abaixo, eu criei variáveis completamente aleatórias sem nenhuma relação entre si e em seguida testo as correlações entre elas. Veja que correlações significativas surgem oriundas do erro tipo I.
make_fake_variables_Teo(nvar = 20)
make_fake_variables_Teo(nvar = 100)
# make_fake_variables_Teo(nvar = 1000)
Na função abaixo (ver “Useful Functions” visualizar o código) eu crio uma um tratamento falso com quatro níveis e quatro repetições e uma variável resposta completamente aleatória. Em seguida, eu substituo quatro valores pela média e refaço a análise para mostrar o enviesamento das estimativas:
show_danger_of_mean_substitution_Teo()
##
##
##
## ############################
## ### Valores Originais ###
## ############################
## Treat Resp
## 1 Trat 1 0.1518
## 2 Trat 1 -0.4019
## 3 Trat 1 2.1214
## 4 Trat 1 0.5016
## 5 Trat 2 1.3720
## 6 Trat 2 0.2510
## 7 Trat 2 -0.2177
## 8 Trat 2 -1.0504
## 9 Trat 3 -0.9679
## 10 Trat 3 -0.7844
## 11 Trat 3 -1.1965
## 12 Trat 3 -0.1383
## 13 Trat 4 1.3887
## 14 Trat 4 1.7278
## 15 Trat 4 -0.4001
## 16 Trat 4 -1.5239
##
##
##
## #####################################
## ### Removendo quatro observacoes ###
## #####################################
## Treat Resp
## 1 Trat 1 NA
## 2 Trat 1 NA
## 3 Trat 1 2.1214
## 4 Trat 1 0.5016
## 5 Trat 2 1.3720
## 6 Trat 2 0.2510
## 7 Trat 2 -0.2177
## 8 Trat 2 -1.0504
## 9 Trat 3 -0.9679
## 10 Trat 3 -0.7844
## 11 Trat 3 -1.1965
## 12 Trat 3 -0.1383
## 13 Trat 4 1.3887
## 14 Trat 4 1.7278
## 15 Trat 4 NA
## 16 Trat 4 NA
##
##
##
## ################################
## ### Mean substitution ###
## ################################
## Treat Resp
## 1 Trat 1 1.3115
## 2 Trat 1 1.3115
## 3 Trat 1 2.1214
## 4 Trat 1 0.5016
## 5 Trat 2 1.3720
## 6 Trat 2 0.2510
## 7 Trat 2 -0.2177
## 8 Trat 2 -1.0504
## 9 Trat 3 -0.9679
## 10 Trat 3 -0.7844
## 11 Trat 3 -1.1965
## 12 Trat 3 -0.1383
## 13 Trat 4 1.3887
## 14 Trat 4 1.7278
## 15 Trat 4 1.5583
## 16 Trat 4 1.5583
##
##
##
## ############################################
## ### Analise com valores verdadeiros ###
## ############################################
## Analysis of Variance Table
##
## Response: Resp
## Df Sum Sq Mean Sq F value Pr(>F)
## Treat 3 4.13 1.38 1.16 0.37
## Residuals 12 14.25 1.19
##
## Call:
## lm(formula = Resp ~ Treat, data = fake)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.822 -0.506 -0.144 0.748 1.528
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.593 0.545 1.09 0.30
## TreatTrat 2 -0.505 0.771 -0.65 0.53
## TreatTrat 3 -1.365 0.771 -1.77 0.10
## TreatTrat 4 -0.295 0.771 -0.38 0.71
##
## Residual standard error: 1.09 on 12 degrees of freedom
## Multiple R-squared: 0.225, Adjusted R-squared: 0.031
## F-statistic: 1.16 on 3 and 12 DF, p-value: 0.365
##
##
##
##
## #############################################################################
## ### Analise com os valores faltantes substituidos pela media do grupo ###
## #############################################################################
## Analysis of Variance Table
##
## Response: Resp
## Df Sum Sq Mean Sq F value Pr(>F)
## Treat 3 14.23 4.74 11.3 0.00084 ***
## Residuals 12 5.05 0.42
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Call:
## lm(formula = Resp ~ Treat, data = fake)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.139 -0.224 0.000 0.164 1.283
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.311 0.324 4.04 0.00163 **
## TreatTrat 2 -1.223 0.459 -2.66 0.02062 *
## TreatTrat 3 -2.083 0.459 -4.54 0.00068 ***
## TreatTrat 4 0.247 0.459 0.54 0.60059
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.649 on 12 degrees of freedom
## Multiple R-squared: 0.738, Adjusted R-squared: 0.672
## F-statistic: 11.3 on 3 and 12 DF, p-value: 0.000838
##
## Number of iterations: 4
“Iterative Robust Model-based Input” através da função irmi do pacote “VIM”
data(sleep)
sleep
## BodyWgt BrainWgt NonD Dream Sleep Span Gest Pred Exp Danger
## 1 6654.000 5712.00 NA NA 3.3 38.6 645.0 3 5 3
## 2 1.000 6.60 6.3 2.0 8.3 4.5 42.0 3 1 3
## 3 3.385 44.50 NA NA 12.5 14.0 60.0 1 1 1
## 4 0.920 5.70 NA NA 16.5 NA 25.0 5 2 3
## 5 2547.000 4603.00 2.1 1.8 3.9 69.0 624.0 3 5 4
## 6 10.550 179.50 9.1 0.7 9.8 27.0 180.0 4 4 4
## 7 0.023 0.30 15.8 3.9 19.7 19.0 35.0 1 1 1
## 8 160.000 169.00 5.2 1.0 6.2 30.4 392.0 4 5 4
## 9 3.300 25.60 10.9 3.6 14.5 28.0 63.0 1 2 1
## 10 52.160 440.00 8.3 1.4 9.7 50.0 230.0 1 1 1
## 11 0.425 6.40 11.0 1.5 12.5 7.0 112.0 5 4 4
## 12 465.000 423.00 3.2 0.7 3.9 30.0 281.0 5 5 5
## 13 0.550 2.40 7.6 2.7 10.3 NA NA 2 1 2
## 14 187.100 419.00 NA NA 3.1 40.0 365.0 5 5 5
## 15 0.075 1.20 6.3 2.1 8.4 3.5 42.0 1 1 1
## 16 3.000 25.00 8.6 0.0 8.6 50.0 28.0 2 2 2
## 17 0.785 3.50 6.6 4.1 10.7 6.0 42.0 2 2 2
## 18 0.200 5.00 9.5 1.2 10.7 10.4 120.0 2 2 2
## 19 1.410 17.50 4.8 1.3 6.1 34.0 NA 1 2 1
## 20 60.000 81.00 12.0 6.1 18.1 7.0 NA 1 1 1
## 21 529.000 680.00 NA 0.3 NA 28.0 400.0 5 5 5
## 22 27.660 115.00 3.3 0.5 3.8 20.0 148.0 5 5 5
## 23 0.120 1.00 11.0 3.4 14.4 3.9 16.0 3 1 2
## 24 207.000 406.00 NA NA 12.0 39.3 252.0 1 4 1
## 25 85.000 325.00 4.7 1.5 6.2 41.0 310.0 1 3 1
## 26 36.330 119.50 NA NA 13.0 16.2 63.0 1 1 1
## 27 0.101 4.00 10.4 3.4 13.8 9.0 28.0 5 1 3
## 28 1.040 5.50 7.4 0.8 8.2 7.6 68.0 5 3 4
## 29 521.000 655.00 2.1 0.8 2.9 46.0 336.0 5 5 5
## 30 100.000 157.00 NA NA 10.8 22.4 100.0 1 1 1
## 31 35.000 56.00 NA NA NA 16.3 33.0 3 5 4
## 32 0.005 0.14 7.7 1.4 9.1 2.6 21.5 5 2 4
## 33 0.010 0.25 17.9 2.0 19.9 24.0 50.0 1 1 1
## 34 62.000 1320.00 6.1 1.9 8.0 100.0 267.0 1 1 1
## 35 0.122 3.00 8.2 2.4 10.6 NA 30.0 2 1 1
## 36 1.350 8.10 8.4 2.8 11.2 NA 45.0 3 1 3
## 37 0.023 0.40 11.9 1.3 13.2 3.2 19.0 4 1 3
## 38 0.048 0.33 10.8 2.0 12.8 2.0 30.0 4 1 3
## 39 1.700 6.30 13.8 5.6 19.4 5.0 12.0 2 1 1
## 40 3.500 10.80 14.3 3.1 17.4 6.5 120.0 2 1 1
## 41 250.000 490.00 NA 1.0 NA 23.6 440.0 5 5 5
## 42 0.480 15.50 15.2 1.8 17.0 12.0 140.0 2 2 2
## 43 10.000 115.00 10.0 0.9 10.9 20.2 170.0 4 4 4
## 44 1.620 11.40 11.9 1.8 13.7 13.0 17.0 2 1 2
## 45 192.000 180.00 6.5 1.9 8.4 27.0 115.0 4 4 4
## 46 2.500 12.10 7.5 0.9 8.4 18.0 31.0 5 5 5
## 47 4.288 39.20 NA NA 12.5 13.7 63.0 2 2 2
## 48 0.280 1.90 10.6 2.6 13.2 4.7 21.0 3 1 3
## 49 4.235 50.40 7.4 2.4 9.8 9.8 52.0 1 1 1
## 50 6.800 179.00 8.4 1.2 9.6 29.0 164.0 2 3 2
## 51 0.750 12.30 5.7 0.9 6.6 7.0 225.0 2 2 2
## 52 3.600 21.00 4.9 0.5 5.4 6.0 225.0 3 2 3
## 53 14.830 98.20 NA NA 2.6 17.0 150.0 5 5 5
## 54 55.500 175.00 3.2 0.6 3.8 20.0 151.0 5 5 5
## 55 1.400 12.50 NA NA 11.0 12.7 90.0 2 2 2
## 56 0.060 1.00 8.1 2.2 10.3 3.5 NA 3 1 2
## 57 0.900 2.60 11.0 2.3 13.3 4.5 60.0 2 1 2
## 58 2.000 12.30 4.9 0.5 5.4 7.5 200.0 3 1 3
## 59 0.104 2.50 13.2 2.6 15.8 2.3 46.0 3 2 2
## 60 4.190 58.00 9.7 0.6 10.3 24.0 210.0 4 3 4
## 61 3.500 3.90 12.8 6.6 19.4 3.0 14.0 2 1 1
## 62 4.050 17.00 NA NA NA 13.0 38.0 3 1 1
summary(sleep)
## BodyWgt BrainWgt NonD Dream
## Min. : 0 Min. : 0 Min. : 2.10 Min. :0.00
## 1st Qu.: 1 1st Qu.: 4 1st Qu.: 6.25 1st Qu.:0.90
## Median : 3 Median : 17 Median : 8.35 Median :1.80
## Mean : 199 Mean : 283 Mean : 8.67 Mean :1.97
## 3rd Qu.: 48 3rd Qu.: 166 3rd Qu.:11.00 3rd Qu.:2.55
## Max. :6654 Max. :5712 Max. :17.90 Max. :6.60
## NA's :14 NA's :12
## Sleep Span Gest Pred
## Min. : 2.60 Min. : 2.00 Min. : 12.0 Min. :1.00
## 1st Qu.: 8.05 1st Qu.: 6.62 1st Qu.: 35.8 1st Qu.:2.00
## Median :10.45 Median : 15.10 Median : 79.0 Median :3.00
## Mean :10.53 Mean : 19.88 Mean :142.4 Mean :2.87
## 3rd Qu.:13.20 3rd Qu.: 27.75 3rd Qu.:207.5 3rd Qu.:4.00
## Max. :19.90 Max. :100.00 Max. :645.0 Max. :5.00
## NA's :4 NA's :4 NA's :4
## Exp Danger
## Min. :1.00 Min. :1.00
## 1st Qu.:1.00 1st Qu.:1.00
## Median :2.00 Median :2.00
## Mean :2.42 Mean :2.61
## 3rd Qu.:4.00 3rd Qu.:4.00
## Max. :5.00 Max. :5.00
##
irmi_sleep <- irmi(sleep)
## Time difference of -0.036 secs
## Imputation performed on the following data set:
## type #missing
## BodyWgt "numeric" "0"
## BrainWgt "numeric" "0"
## NonD "numeric" "14"
## Dream "numeric" "12"
## Sleep "numeric" "4"
## Span "numeric" "4"
## Gest "numeric" "4"
## Pred "integer" "0"
## Exp "integer" "0"
## Danger "integer" "0"
irmi_sleep
## BodyWgt BrainWgt NonD Dream Sleep Span Gest Pred Exp Danger
## 1 6654.000 5712.00 2.881 0.68214 3.300 38.600 645.00 3 5 3
## 2 1.000 6.60 6.300 2.00000 8.300 4.500 42.00 3 1 3
## 3 3.385 44.50 9.840 2.66425 12.500 14.000 60.00 1 1 1
## 4 0.920 5.70 13.592 2.90062 16.500 -4.643 25.00 5 2 3
## 5 2547.000 4603.00 2.100 1.80000 3.900 69.000 624.00 3 5 4
## 6 10.550 179.50 9.100 0.70000 9.800 27.000 180.00 4 4 4
## 7 0.023 0.30 15.800 3.90000 19.700 19.000 35.00 1 1 1
## 8 160.000 169.00 5.200 1.00000 6.200 30.400 392.00 4 5 4
## 9 3.300 25.60 10.900 3.60000 14.500 28.000 63.00 1 2 1
## 10 52.160 440.00 8.300 1.40000 9.700 50.000 230.00 1 1 1
## 11 0.425 6.40 11.000 1.50000 12.500 7.000 112.00 5 4 4
## 12 465.000 423.00 3.200 0.70000 3.900 30.000 281.00 5 5 5
## 13 0.550 2.40 7.600 2.70000 10.300 -7.674 104.74 2 1 2
## 14 187.100 419.00 3.016 0.08366 3.100 40.000 365.00 5 5 5
## 15 0.075 1.20 6.300 2.10000 8.400 3.500 42.00 1 1 1
## 16 3.000 25.00 8.600 0.00000 8.600 50.000 28.00 2 2 2
## 17 0.785 3.50 6.600 4.10000 10.700 6.000 42.00 2 2 2
## 18 0.200 5.00 9.500 1.20000 10.700 10.400 120.00 2 2 2
## 19 1.410 17.50 4.800 1.30000 6.100 34.000 44.60 1 2 1
## 20 60.000 81.00 12.000 6.10000 18.100 7.000 49.11 1 1 1
## 21 529.000 680.00 4.074 0.30000 4.377 28.000 400.00 5 5 5
## 22 27.660 115.00 3.300 0.50000 3.800 20.000 148.00 5 5 5
## 23 0.120 1.00 11.000 3.40000 14.400 3.900 16.00 3 1 2
## 24 207.000 406.00 9.299 2.70040 12.000 39.300 252.00 1 4 1
## 25 85.000 325.00 4.700 1.50000 6.200 41.000 310.00 1 3 1
## 26 36.330 119.50 10.292 2.70505 13.000 16.200 63.00 1 1 1
## 27 0.101 4.00 10.400 3.40000 13.800 9.000 28.00 5 1 3
## 28 1.040 5.50 7.400 0.80000 8.200 7.600 68.00 5 3 4
## 29 521.000 655.00 2.100 0.80000 2.900 46.000 336.00 5 5 5
## 30 100.000 157.00 9.318 1.48558 10.800 22.400 100.00 1 1 1
## 31 35.000 56.00 10.314 2.22613 12.548 16.300 33.00 3 5 4
## 32 0.005 0.14 7.700 1.40000 9.100 2.600 21.50 5 2 4
## 33 0.010 0.25 17.900 2.00000 19.900 24.000 50.00 1 1 1
## 34 62.000 1320.00 6.100 1.90000 8.000 100.000 267.00 1 1 1
## 35 0.122 3.00 8.200 2.40000 10.600 -1.370 30.00 2 1 1
## 36 1.350 8.10 8.400 2.80000 11.200 -15.397 45.00 3 1 3
## 37 0.023 0.40 11.900 1.30000 13.200 3.200 19.00 4 1 3
## 38 0.048 0.33 10.800 2.00000 12.800 2.000 30.00 4 1 3
## 39 1.700 6.30 13.800 5.60000 19.400 5.000 12.00 2 1 1
## 40 3.500 10.80 14.300 3.10000 17.400 6.500 120.00 2 1 1
## 41 250.000 490.00 5.447 1.00000 6.453 23.600 440.00 5 5 5
## 42 0.480 15.50 15.200 1.80000 17.000 12.000 140.00 2 2 2
## 43 10.000 115.00 10.000 0.90000 10.900 20.200 170.00 4 4 4
## 44 1.620 11.40 11.900 1.80000 13.700 13.000 17.00 2 1 2
## 45 192.000 180.00 6.500 1.90000 8.400 27.000 115.00 4 4 4
## 46 2.500 12.10 7.500 0.90000 8.400 18.000 31.00 5 5 5
## 47 4.288 39.20 9.965 2.52422 12.500 13.700 63.00 2 2 2
## 48 0.280 1.90 10.600 2.60000 13.200 4.700 21.00 3 1 3
## 49 4.235 50.40 7.400 2.40000 9.800 9.800 52.00 1 1 1
## 50 6.800 179.00 8.400 1.20000 9.600 29.000 164.00 2 3 2
## 51 0.750 12.30 5.700 0.90000 6.600 7.000 225.00 2 2 2
## 52 3.600 21.00 4.900 0.50000 5.400 6.000 225.00 3 2 3
## 53 14.830 98.20 2.968 -0.37995 2.600 17.000 150.00 5 5 5
## 54 55.500 175.00 3.200 0.60000 3.800 20.000 151.00 5 5 5
## 55 1.400 12.50 9.169 1.81922 11.000 12.700 90.00 2 2 2
## 56 0.060 1.00 8.100 2.20000 10.300 3.500 75.87 3 1 2
## 57 0.900 2.60 11.000 2.30000 13.300 4.500 60.00 2 1 2
## 58 2.000 12.30 4.900 0.50000 5.400 7.500 200.00 3 1 3
## 59 0.104 2.50 13.200 2.60000 15.800 2.300 46.00 3 2 2
## 60 4.190 58.00 9.700 0.60000 10.300 24.000 210.00 4 3 4
## 61 3.500 3.90 12.800 6.60000 19.400 3.000 14.00 2 1 1
## 62 4.050 17.00 6.113 2.07369 8.162 13.000 38.00 3 1 1
“Bootstrapped expectation-maximization” através da função amelia do pacote Amelia
amelia_sleep <- amelia(sleep, bounds = matrix(c(3, 0, 20), ncol = 3))
amelia_sleep <- amelia_sleep$imputations$imp5
amelia_sleep
## BodyWgt BrainWgt NonD Dream Sleep Span Gest Pred Exp Danger
## 1 6654.000 5712.00 0.000 7.5987 3.300 38.600 645.00 3 5 3
## 2 1.000 6.60 6.300 2.0000 8.300 4.500 42.00 3 1 3
## 3 3.385 44.50 9.619 2.8810 12.500 14.000 60.00 1 1 1
## 4 0.920 5.70 12.435 4.0653 16.500 -6.118 25.00 5 2 3
## 5 2547.000 4603.00 2.100 1.8000 3.900 69.000 624.00 3 5 4
## 6 10.550 179.50 9.100 0.7000 9.800 27.000 180.00 4 4 4
## 7 0.023 0.30 15.800 3.9000 19.700 19.000 35.00 1 1 1
## 8 160.000 169.00 5.200 1.0000 6.200 30.400 392.00 4 5 4
## 9 3.300 25.60 10.900 3.6000 14.500 28.000 63.00 1 2 1
## 10 52.160 440.00 8.300 1.4000 9.700 50.000 230.00 1 1 1
## 11 0.425 6.40 11.000 1.5000 12.500 7.000 112.00 5 4 4
## 12 465.000 423.00 3.200 0.7000 3.900 30.000 281.00 5 5 5
## 13 0.550 2.40 7.600 2.7000 10.300 1.500 138.96 2 1 2
## 14 187.100 419.00 3.209 -0.1087 3.100 40.000 365.00 5 5 5
## 15 0.075 1.20 6.300 2.1000 8.400 3.500 42.00 1 1 1
## 16 3.000 25.00 8.600 0.0000 8.600 50.000 28.00 2 2 2
## 17 0.785 3.50 6.600 4.1000 10.700 6.000 42.00 2 2 2
## 18 0.200 5.00 9.500 1.2000 10.700 10.400 120.00 2 2 2
## 19 1.410 17.50 4.800 1.3000 6.100 34.000 125.21 1 2 1
## 20 60.000 81.00 12.000 6.1000 18.100 7.000 82.23 1 1 1
## 21 529.000 680.00 1.049 0.3000 1.349 28.000 400.00 5 5 5
## 22 27.660 115.00 3.300 0.5000 3.800 20.000 148.00 5 5 5
## 23 0.120 1.00 11.000 3.4000 14.400 3.900 16.00 3 1 2
## 24 207.000 406.00 8.270 3.7313 12.000 39.300 252.00 1 4 1
## 25 85.000 325.00 4.700 1.5000 6.200 41.000 310.00 1 3 1
## 26 36.330 119.50 11.923 1.0779 13.000 16.200 63.00 1 1 1
## 27 0.101 4.00 10.400 3.4000 13.800 9.000 28.00 5 1 3
## 28 1.040 5.50 7.400 0.8000 8.200 7.600 68.00 5 3 4
## 29 521.000 655.00 2.100 0.8000 2.900 46.000 336.00 5 5 5
## 30 100.000 157.00 7.595 3.2042 10.800 22.400 100.00 1 1 1
## 31 35.000 56.00 5.671 3.3877 9.057 16.300 33.00 3 5 4
## 32 0.005 0.14 7.700 1.4000 9.100 2.600 21.50 5 2 4
## 33 0.010 0.25 17.900 2.0000 19.900 24.000 50.00 1 1 1
## 34 62.000 1320.00 6.100 1.9000 8.000 100.000 267.00 1 1 1
## 35 0.122 3.00 8.200 2.4000 10.600 22.391 30.00 2 1 1
## 36 1.350 8.10 8.400 2.8000 11.200 1.177 45.00 3 1 3
## 37 0.023 0.40 11.900 1.3000 13.200 3.200 19.00 4 1 3
## 38 0.048 0.33 10.800 2.0000 12.800 2.000 30.00 4 1 3
## 39 1.700 6.30 13.800 5.6000 19.400 5.000 12.00 2 1 1
## 40 3.500 10.80 14.300 3.1000 17.400 6.500 120.00 2 1 1
## 41 250.000 490.00 2.726 1.0000 3.727 23.600 440.00 5 5 5
## 42 0.480 15.50 15.200 1.8000 17.000 12.000 140.00 2 2 2
## 43 10.000 115.00 10.000 0.9000 10.900 20.200 170.00 4 4 4
## 44 1.620 11.40 11.900 1.8000 13.700 13.000 17.00 2 1 2
## 45 192.000 180.00 6.500 1.9000 8.400 27.000 115.00 4 4 4
## 46 2.500 12.10 7.500 0.9000 8.400 18.000 31.00 5 5 5
## 47 4.288 39.20 10.752 1.7472 12.500 13.700 63.00 2 2 2
## 48 0.280 1.90 10.600 2.6000 13.200 4.700 21.00 3 1 3
## 49 4.235 50.40 7.400 2.4000 9.800 9.800 52.00 1 1 1
## 50 6.800 179.00 8.400 1.2000 9.600 29.000 164.00 2 3 2
## 51 0.750 12.30 5.700 0.9000 6.600 7.000 225.00 2 2 2
## 52 3.600 21.00 4.900 0.5000 5.400 6.000 225.00 3 2 3
## 53 14.830 98.20 2.446 0.1542 2.600 17.000 150.00 5 5 5
## 54 55.500 175.00 3.200 0.6000 3.800 20.000 151.00 5 5 5
## 55 1.400 12.50 9.384 1.6171 11.000 12.700 90.00 2 2 2
## 56 0.060 1.00 8.100 2.2000 10.300 3.500 72.63 3 1 2
## 57 0.900 2.60 11.000 2.3000 13.300 4.500 60.00 2 1 2
## 58 2.000 12.30 4.900 0.5000 5.400 7.500 200.00 3 1 3
## 59 0.104 2.50 13.200 2.6000 15.800 2.300 46.00 3 2 2
## 60 4.190 58.00 9.700 0.6000 10.300 24.000 210.00 4 3 4
## 61 3.500 3.90 12.800 6.6000 19.400 3.000 14.00 2 1 1
## 62 4.050 17.00 15.873 4.5217 20.397 13.000 38.00 3 1 1
cbind(sleep[, 3, drop = FALSE], irmi_sleep[, 3, drop = FALSE], amelia_sleep[,
3, drop = FALSE])
## NonD NonD NonD
## 1 NA 2.881 0.000
## 2 6.3 6.300 6.300
## 3 NA 9.840 9.619
## 4 NA 13.592 12.435
## 5 2.1 2.100 2.100
## 6 9.1 9.100 9.100
## 7 15.8 15.800 15.800
## 8 5.2 5.200 5.200
## 9 10.9 10.900 10.900
## 10 8.3 8.300 8.300
## 11 11.0 11.000 11.000
## 12 3.2 3.200 3.200
## 13 7.6 7.600 7.600
## 14 NA 3.016 3.209
## 15 6.3 6.300 6.300
## 16 8.6 8.600 8.600
## 17 6.6 6.600 6.600
## 18 9.5 9.500 9.500
## 19 4.8 4.800 4.800
## 20 12.0 12.000 12.000
## 21 NA 4.074 1.049
## 22 3.3 3.300 3.300
## 23 11.0 11.000 11.000
## 24 NA 9.299 8.270
## 25 4.7 4.700 4.700
## 26 NA 10.292 11.923
## 27 10.4 10.400 10.400
## 28 7.4 7.400 7.400
## 29 2.1 2.100 2.100
## 30 NA 9.318 7.595
## 31 NA 10.314 5.671
## 32 7.7 7.700 7.700
## 33 17.9 17.900 17.900
## 34 6.1 6.100 6.100
## 35 8.2 8.200 8.200
## 36 8.4 8.400 8.400
## 37 11.9 11.900 11.900
## 38 10.8 10.800 10.800
## 39 13.8 13.800 13.800
## 40 14.3 14.300 14.300
## 41 NA 5.447 2.726
## 42 15.2 15.200 15.200
## 43 10.0 10.000 10.000
## 44 11.9 11.900 11.900
## 45 6.5 6.500 6.500
## 46 7.5 7.500 7.500
## 47 NA 9.965 10.752
## 48 10.6 10.600 10.600
## 49 7.4 7.400 7.400
## 50 8.4 8.400 8.400
## 51 5.7 5.700 5.700
## 52 4.9 4.900 4.900
## 53 NA 2.968 2.446
## 54 3.2 3.200 3.200
## 55 NA 9.169 9.384
## 56 8.1 8.100 8.100
## 57 11.0 11.000 11.000
## 58 4.9 4.900 4.900
## 59 13.2 13.200 13.200
## 60 9.7 9.700 9.700
## 61 12.8 12.800 12.800
## 62 NA 6.113 15.873