setwd("C:/Users/aluno/Documents")
metais<-read.table("metais.txt", h=TRUE)
library(car)
## Warning: package 'car' was built under R version 3.1.3
library (corrplot)
## Warning: package 'corrplot' was built under R version 3.1.3
# Verificando os dados
str (metais)
## 'data.frame': 21 obs. of 6 variables:
## $ Inf.Pb: num 18 3 4 24 35 31 32 14 40 27 ...
## $ Eff.Pb: num 3 1 1 21 34 2 4 6 6 9 ...
## $ Inf.Ni: num 33 47 26 33 23 28 36 41 47 42 ...
## $ Eff.Ni: num 25 41 8 27 10 16 19 43 18 16 ...
## $ Inf.Zn: num 194 291 234 225 160 223 206 135 329 221 ...
## $ Eff.Zn: num 96 81 63 65 31 41 40 47 72 72 ...
summary (metais)
## Inf.Pb Eff.Pb Inf.Ni Eff.Ni
## Min. : 3.00 Min. : 1.000 Min. :13.00 Min. : 3.00
## 1st Qu.:14.00 1st Qu.: 4.000 1st Qu.:24.00 1st Qu.:14.00
## Median :23.00 Median : 6.000 Median :33.00 Median :18.00
## Mean :21.05 Mean : 8.238 Mean :34.05 Mean :21.86
## 3rd Qu.:28.00 3rd Qu.: 9.000 3rd Qu.:42.00 3rd Qu.:25.00
## Max. :40.00 Max. :34.000 Max. :69.00 Max. :63.00
## Inf.Zn Eff.Zn
## Min. :135.0 Min. :31.0
## 1st Qu.:207.0 1st Qu.:49.0
## Median :234.0 Median :61.0
## Mean :251.8 Mean :61.1
## 3rd Qu.:267.0 3rd Qu.:72.0
## Max. :464.0 Max. :96.0
names (metais)
## [1] "Inf.Pb" "Eff.Pb" "Inf.Ni" "Eff.Ni" "Inf.Zn" "Eff.Zn"
### 1) Análise grafica
pairs(metais)

## Usando a função scatterplotMatrix do pacote car
scatterplotMatrix(metais, spread=FALSE, smoother.args=list(lty=2))

## Usando a função corrplot
corrplot(cor(metais), type="upper", tl.col="black", tl.srt=4)

### 2) Matriz de Correlações
cor(metais)
## Inf.Pb Eff.Pb Inf.Ni Eff.Ni Inf.Zn
## Inf.Pb 1.00000000 0.2157941 0.32276440 0.01812931 0.01765980
## Eff.Pb 0.21579410 1.0000000 -0.28512662 -0.20773675 -0.19800187
## Inf.Ni 0.32276440 -0.2851266 1.00000000 0.24432850 -0.05459522
## Eff.Ni 0.01812931 -0.2077367 0.24432850 1.00000000 -0.04786391
## Inf.Zn 0.01765980 -0.1980019 -0.05459522 -0.04786391 1.00000000
## Eff.Zn -0.16340908 -0.4262317 0.47898286 0.19415646 0.16986079
## Eff.Zn
## Inf.Pb -0.1634091
## Eff.Pb -0.4262317
## Inf.Ni 0.4789829
## Eff.Ni 0.1941565
## Inf.Zn 0.1698608
## Eff.Zn 1.0000000
### Estimativas pontuais e ICs
cor.test(metais$Inf.Pb, metais$Eff.Pb, conf.level = 0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Pb and metais$Eff.Pb
## t = 0.9633, df = 19, p-value = 0.3475
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.2380702 0.5923045
## sample estimates:
## cor
## 0.2157941
cor.test(metais$Inf.Pb, metais$Eff.Pb, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Pb and metais$Eff.Pb
## t = 0.9633, df = 19, p-value = 0.3475
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.2380702 0.5923045
## sample estimates:
## cor
## 0.2157941
cor.test(metais$Inf.Zn, metais$Eff.Zn, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Zn and metais$Eff.Zn
## t = 0.7513, df = 19, p-value = 0.4617
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.2825440 0.5604515
## sample estimates:
## cor
## 0.1698608
cor.test(metais$Inf.Ni, metais$Eff.Ni, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Ni and metais$Eff.Ni
## t = 1.0983, df = 19, p-value = 0.2858
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.2094496 0.6115166
## sample estimates:
## cor
## 0.2443285
cor.test(metais$Inf.Pb, metais$Eff.Zn, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Pb and metais$Eff.Zn
## t = -0.722, df = 19, p-value = 0.4791
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.5558830 0.2886387
## sample estimates:
## cor
## -0.1634091
cor.test(metais$Inf.Pb, metais$Eff.Ni, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Pb and metais$Eff.Ni
## t = 0.079, df = 19, p-value = 0.9378
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.4168196 0.4463231
## sample estimates:
## cor
## 0.01812931
cor.test(metais$Inf.Ni, metais$Eff.Pb, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Ni and metais$Eff.Pb
## t = -1.2967, df = 19, p-value = 0.2103
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.6382537 0.1671316
## sample estimates:
## cor
## -0.2851266
cor.test(metais$Inf.Ni, metais$Eff.Zn, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Ni and metais$Eff.Zn
## t = 2.3784, df = 19, p-value = 0.02803
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.05962472 0.75463362
## sample estimates:
## cor
## 0.4789829
cor.test(metais$Inf.Zn, metais$Eff.Pb, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Zn and metais$Eff.Pb
## t = -0.8805, df = 19, p-value = 0.3896
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.5801044 0.2555259
## sample estimates:
## cor
## -0.1980019
cor.test(metais$Inf.Zn, metais$Eff.Ni, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Inf.Zn and metais$Eff.Ni
## t = -0.2089, df = 19, p-value = 0.8368
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.4698427 0.3919208
## sample estimates:
## cor
## -0.04786391
cor.test(metais$Eff.Pb, metais$Eff.Zn, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Eff.Pb and metais$Eff.Zn
## t = -2.0538, df = 19, p-value = 0.05402
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.724594181 0.006685118
## sample estimates:
## cor
## -0.4262317
cor.test(metais$Eff.Pb, metais$Eff.Ni, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Eff.Pb and metais$Eff.Ni
## t = -0.9257, df = 19, p-value = 0.3662
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.5868009 0.2460117
## sample estimates:
## cor
## -0.2077367
cor.test(metais$Eff.Zn, metais$Eff.Ni, conf.level=0.95)
##
## Pearson's product-moment correlation
##
## data: metais$Eff.Zn and metais$Eff.Ni
## t = 0.8627, df = 19, p-value = 0.399
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.2592602 0.5774448
## sample estimates:
## cor
## 0.1941565
Exercícios do Devore
library(Devore7)
## Warning: package 'Devore7' was built under R version 3.1.3
## Loading required package: MASS
## Loading required package: lattice
# Ex. 84, Ex. Complementar, Cap. 12
data(ex12.84)
str(ex12.84)
## 'data.frame': 20 obs. of 2 variables:
## $ HW : num 8 6.2 9.2 6.4 8.6 12.2 7.2 12 14.9 12.1 ...
## $ BOD: num 2.5 4 4.1 6.2 7.1 7 8.3 9.2 9.3 12 ...
#Grafico de Dispersão com ggplot2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.1.3
qplot(HW, BOD, data = ex12.84, geom = c("point", "smooth"), method = "lm") + theme_bw()

# Quantificando a correlação
cor.test(ex12.84$HW, ex12.84$BOD, conf.level = 0.95)
##
## Pearson's product-moment correlation
##
## data: ex12.84$HW and ex12.84$BOD
## t = 7.4119, df = 18, p-value = 7.144e-07
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6905699 0.9467818
## sample estimates:
## cor
## 0.8678753
#Estimando o modelo de regressão linear
mreg1=lm(BOD ~ HW, data=ex12.84)
summary(mreg1)
##
## Call:
## lm(formula = BOD ~ HW, data = ex12.84)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.6947 -1.5141 -0.0957 1.8149 4.7134
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.1052 1.8715 -1.125 0.275
## HW 1.0134 0.1367 7.412 7.14e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.506 on 18 degrees of freedom
## Multiple R-squared: 0.7532, Adjusted R-squared: 0.7395
## F-statistic: 54.94 on 1 and 18 DF, p-value: 7.144e-07
#Diagnóstico do modelo
plot(mreg1, which=1)

plot(mreg1, which=2)

# Ex. 47. Seção 13.4, Cap. 13
data(ex13.47)
str(ex13.47)
## 'data.frame': 30 obs. of 6 variables:
## $ Row : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Plastics : num 18.7 19.4 19.2 22.6 16.5 ...
## $ Paper : num 15.6 23.5 24.2 22.2 23.6 ...
## $ Garbage : num 45 39.7 43.2 35.8 41.2 ...
## $ Water : num 58.2 46.3 46.6 45.9 55.1 ...
## $ Energy.content: int 947 1407 1452 1553 989 1162 1466 1656 1254 1336 ...
# Definindo o valor de X para a previsão
novox = data.frame(HW = 15)
# predict = intervalos de previsão para Y
predict(mreg1,novox, interval="predict")
## fit lwr upr
## 1 13.09603 7.672846 18.51921
#confint = estimativas de IC para os parâmetros
confint(mreg1, level=0.95)
## 2.5 % 97.5 %
## (Intercept) -6.0371353 1.826710
## HW 0.7261594 1.300673
# Ex. 47, seçãO 13.4 Cap. 13
data(ex13.47)
str(ex13.47)
## 'data.frame': 30 obs. of 6 variables:
## $ Row : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Plastics : num 18.7 19.4 19.2 22.6 16.5 ...
## $ Paper : num 15.6 23.5 24.2 22.2 23.6 ...
## $ Garbage : num 45 39.7 43.2 35.8 41.2 ...
## $ Water : num 58.2 46.3 46.6 45.9 55.1 ...
## $ Energy.content: int 947 1407 1452 1553 989 1162 1466 1656 1254 1336 ...
mreg2=lm(Energy.content ~ Plastics + Paper + Garbage + Water, data=ex13.47)
summary(mreg2)
##
## Call:
## lm(formula = Energy.content ~ Plastics + Paper + Garbage + Water,
## data = ex13.47)
##
## Residuals:
## Min 1Q Median 3Q Max
## -41.34 -24.04 -11.00 22.54 59.73
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2245.093 177.892 12.621 2.42e-12 ***
## Plastics 28.922 2.823 10.243 1.97e-10 ***
## Paper 7.643 2.314 3.303 0.00288 **
## Garbage 4.297 1.916 2.243 0.03404 *
## Water -37.356 1.834 -20.367 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 31.48 on 25 degrees of freedom
## Multiple R-squared: 0.9641, Adjusted R-squared: 0.9583
## F-statistic: 167.7 on 4 and 25 DF, p-value: < 2.2e-16