options(repos = c(CRAN = "https://cloud.r-project.org/"))
library(UsingR)
## Loading required package: MASS
## Loading required package: HistData
## Loading required package: Hmisc
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
library(MASS)
punto 2.2 a)
hist(brightness, probability = TRUE)
lines(density(brightness), col = "red")
punto 2.2 b
par(mar=c(5, 4, 4, 2) + 0.1)
boxplot(brightness)
bigote_inferior=quantile(brightness,0.25)-1.5*IQR(brightness)
bigote_superior=quantile(brightness,0.75)+ 1.5 *IQR(brightness)
outliers=brightness[brightness < bigote_inferior | brightness > bigote_superior]
print(outliers)
## [1] 12.31 11.71 5.53 11.28 4.78 5.13 4.37 5.04 12.43 12.04 4.55 11.55
## [13] 12.14 11.63 4.99 11.67 4.61 11.99 12.04 5.55 12.17 11.55 11.79 12.19
## [25] 2.07 11.65 11.73 2.28 5.42 3.88 5.54 5.29 5.01 11.55 4.89 11.80
## [37] 5.41 5.24
punto 2.2 c)
qnt <- quantile(brightness, probs=c(.25, .75), na.rm = T)
iqr <- IQR(brightness)
brightness_without <- brightness[brightness > (qnt[1] - 1.5*iqr) & brightness < (qnt[2] + 1.5*iqr)]
Punto 2.3 i
table(UScereal$mfr,UScereal$shelf)
##
## 1 2 3
## G 6 7 9
## K 4 7 10
## N 2 0 1
## P 2 1 6
## Q 0 3 2
## R 4 0 1
Punto 2.3 ii
table(UScereal$fat,UScereal$vitamins)
##
## 100% enriched none
## 0 1 18 3
## 0.6666667 0 1 0
## 1 3 7 0
## 1.1363636 0 1 0
## 1.3333333 1 8 0
## 1.4925373 0 4 0
## 1.6 0 1 0
## 2 0 2 0
## 2.6666667 0 3 0
## 2.9850746 0 4 0
## 3.030303 0 2 0
## 4 0 4 0
## 6 0 1 0
## 9.0909091 0 1 0
Punto 2.3 iii
table(UScereal$fat,UScereal$shelf)
##
## 1 2 3
## 0 10 3 9
## 0.6666667 0 1 0
## 1 2 5 3
## 1.1363636 0 0 1
## 1.3333333 2 4 3
## 1.4925373 2 1 1
## 1.6 1 0 0
## 2 0 1 1
## 2.6666667 1 1 1
## 2.9850746 0 1 3
## 3.030303 0 0 2
## 4 0 1 3
## 6 0 0 1
## 9.0909091 0 0 1
punto 2.3 iv
table(UScereal$carbo,UScereal$sugars)
##
## 0 0.8 1.769912 2 3 4 4.477612 5.681818 6 6.666667 7.462687 8.270677
## 10.52632 0 0 0 0 0 0 0 0 0 0 0 1
## 11 0 0 0 0 0 0 0 0 0 0 0 0
## 12 0 0 0 0 0 0 0 0 0 0 0 0
## 12.5 0 0 0 0 0 0 0 0 0 0 0 0
## 13 1 0 0 0 0 0 0 0 0 0 0 0
## 13.6 0 1 0 0 0 0 0 0 0 0 0 0
## 14 0 0 0 1 0 0 0 0 0 0 0 0
## 14.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 15 0 0 0 0 0 0 0 0 1 0 0 0
## 15.15152 0 0 0 0 0 0 0 0 0 0 0 0
## 15.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 16 1 0 0 0 2 0 0 0 0 0 0 0
## 16.41791 0 0 0 0 0 0 0 0 0 0 0 0
## 17 0 0 0 0 1 0 0 0 0 0 0 0
## 17.04545 0 0 0 0 0 0 0 1 0 0 0 0
## 17.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 17.5 0 0 0 0 0 0 0 0 0 0 0 0
## 17.91045 0 0 0 0 0 0 0 0 0 0 0 0
## 18.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 19.40299 0 0 0 0 0 0 0 0 0 0 1 0
## 20 0 0 0 0 1 0 0 0 0 0 0 0
## 20.35398 0 0 1 0 0 0 0 0 0 0 0 0
## 20.89552 0 0 0 0 0 0 0 0 0 0 0 0
## 21 0 0 0 1 2 0 0 0 0 0 0 0
## 21.21212 0 0 0 0 0 0 0 0 0 0 0 0
## 21.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 22 0 0 0 0 2 0 0 0 0 0 0 0
## 22.38806 0 0 0 0 0 0 0 0 0 0 0 0
## 24 0 0 0 0 0 0 0 0 0 1 0 0
## 25.37313 0 0 0 0 0 0 1 0 0 0 0 0
## 26 0 0 0 0 0 0 0 0 0 0 0 0
## 26.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 27 0 0 0 0 0 0 0 0 0 0 0 0
## 28 0 0 0 0 0 1 0 0 0 0 0 0
## 28.35821 1 0 0 0 0 0 0 0 0 0 0 0
## 29.85075 1 0 0 0 0 0 0 0 0 0 0 0
## 30 0 0 0 0 0 0 0 0 0 0 0 0
## 31.34328 0 0 0 0 0 0 0 0 0 0 0 0
## 39.39394 0 0 0 0 0 0 0 0 0 0 0 0
## 68 0 0 0 0 0 0 0 0 0 0 0 0
##
## 8.75 8.955224 10.447761 10.666667 11 12 12.121212 13 13.333333
## 10.52632 0 0 0 0 0 0 0 0 0
## 11 0 0 0 0 0 0 0 1 0
## 12 0 0 0 0 1 1 0 2 0
## 12.5 0 0 0 0 0 0 0 0 0
## 13 0 0 0 0 0 2 0 0 0
## 13.6 0 0 0 0 0 0 0 0 0
## 14 0 0 0 0 0 0 0 0 1
## 14.66667 0 0 0 0 0 0 0 0 1
## 15 0 0 0 0 0 0 0 0 0
## 15.15152 0 0 0 0 0 0 0 0 0
## 15.33333 0 0 0 0 0 0 0 0 1
## 16 0 0 0 0 0 0 0 0 0
## 16.41791 0 0 0 0 0 0 0 0 0
## 17 0 0 0 0 0 0 0 0 0
## 17.04545 0 0 0 0 0 0 0 0 0
## 17.33333 0 0 0 0 0 1 0 0 0
## 17.5 1 0 0 0 0 0 0 0 0
## 17.91045 0 1 0 0 0 0 0 0 0
## 18.66667 0 0 0 0 0 0 0 0 0
## 19.40299 0 0 0 0 0 0 0 0 0
## 20 0 0 0 0 0 1 0 0 0
## 20.35398 0 0 0 0 0 0 0 0 0
## 20.89552 0 0 0 0 0 0 0 0 0
## 21 0 0 0 0 0 0 0 0 0
## 21.21212 0 0 0 0 0 0 0 0 0
## 21.33333 0 0 0 1 0 0 0 0 0
## 22 0 0 0 0 0 0 0 0 0
## 22.38806 0 1 0 0 0 0 0 0 0
## 24 0 0 0 1 0 0 0 0 0
## 25.37313 0 0 0 0 0 0 0 0 0
## 26 0 0 0 0 0 0 0 0 0
## 26.66667 0 0 0 0 0 1 0 0 0
## 27 0 0 0 0 0 0 0 0 0
## 28 0 0 0 0 0 1 0 0 0
## 28.35821 0 0 0 0 0 0 0 0 0
## 29.85075 0 0 0 0 0 0 0 0 0
## 30 0 0 0 0 0 1 0 0 0
## 31.34328 0 0 1 0 0 0 0 0 0
## 39.39394 0 0 0 0 0 0 1 0 0
## 68 0 0 0 0 0 1 0 0 0
##
## 13.432836 14 14.666667 14.925373 15.151515 16 17.045455 17.910448
## 10.52632 0 0 0 0 0 0 0 0
## 11 0 1 0 0 0 0 0 0
## 12 0 0 0 0 0 0 0 0
## 12.5 0 0 0 0 0 0 1 0
## 13 0 0 0 0 0 0 0 0
## 13.6 0 0 0 0 0 0 0 0
## 14 0 0 0 0 0 0 0 0
## 14.66667 0 0 0 0 0 0 0 0
## 15 0 1 0 0 0 0 0 0
## 15.15152 0 0 0 0 0 0 0 0
## 15.33333 0 0 0 0 0 0 0 0
## 16 0 0 0 0 0 1 0 0
## 16.41791 0 0 0 0 0 0 0 0
## 17 0 0 0 0 0 0 0 0
## 17.04545 0 0 0 0 0 0 0 0
## 17.33333 0 0 0 0 0 1 0 0
## 17.5 0 0 0 0 0 0 0 0
## 17.91045 0 0 0 1 0 0 0 0
## 18.66667 0 0 1 0 0 1 0 0
## 19.40299 0 0 0 0 0 0 0 0
## 20 0 1 0 0 0 0 0 0
## 20.35398 0 0 0 0 0 0 0 0
## 20.89552 0 0 0 0 0 0 0 1
## 21 0 0 0 0 0 1 0 0
## 21.21212 0 0 0 0 1 0 0 0
## 21.33333 0 0 0 0 0 0 0 0
## 22 0 0 0 0 0 0 0 0
## 22.38806 1 0 0 0 0 0 0 0
## 24 0 0 0 0 0 0 0 0
## 25.37313 0 0 0 0 0 0 0 0
## 26 0 1 0 0 0 0 0 0
## 26.66667 0 0 0 0 0 0 0 0
## 27 0 0 0 0 0 0 0 0
## 28 0 0 0 0 0 0 0 0
## 28.35821 0 0 0 0 0 0 0 0
## 29.85075 0 0 0 0 0 0 0 0
## 30 0 0 0 0 0 0 0 0
## 31.34328 0 0 0 0 0 0 0 0
## 39.39394 0 0 0 0 0 0 0 0
## 68 0 0 0 0 0 0 0 0
##
## 18.181818 19.402985 20 20.895522
## 10.52632 0 0 0 0
## 11 0 0 0 0
## 12 0 0 1 0
## 12.5 0 0 0 0
## 13 0 0 0 0
## 13.6 0 0 0 0
## 14 0 0 0 0
## 14.66667 0 0 0 0
## 15 0 0 0 0
## 15.15152 1 0 0 0
## 15.33333 0 0 0 0
## 16 0 0 0 0
## 16.41791 0 0 0 1
## 17 0 0 0 0
## 17.04545 0 0 0 0
## 17.33333 0 0 0 0
## 17.5 0 0 0 0
## 17.91045 0 0 0 0
## 18.66667 0 0 0 0
## 19.40299 0 0 0 0
## 20 0 0 0 0
## 20.35398 0 0 0 0
## 20.89552 0 0 0 0
## 21 0 0 0 0
## 21.21212 0 0 0 0
## 21.33333 0 0 0 0
## 22 0 0 0 0
## 22.38806 0 0 0 0
## 24 0 0 0 0
## 25.37313 0 1 0 0
## 26 0 0 0 0
## 26.66667 0 0 0 0
## 27 0 0 1 0
## 28 0 0 0 0
## 28.35821 0 0 0 0
## 29.85075 0 0 0 0
## 30 0 0 0 0
## 31.34328 0 0 0 0
## 39.39394 0 0 0 0
## 68 0 0 0 0
punto 2.3 v
table(UScereal$fibre,UScereal$mfr)
##
## G K N P Q R
## 0 9 2 0 3 2 2
## 1 0 7 0 0 1 0
## 1.333333 1 2 0 0 0 1
## 1.6 1 0 0 0 0 0
## 2 3 0 0 0 0 0
## 2.666667 2 1 0 0 0 0
## 2.985075 0 0 0 0 1 0
## 3 3 0 0 0 0 0
## 3.409091 0 0 0 1 0 0
## 3.75 0 1 0 0 0 0
## 4 2 1 0 0 1 0
## 4.477612 0 2 1 0 0 1
## 5 1 0 0 0 0 0
## 5.970149 0 0 1 0 0 1
## 6.666667 0 1 0 0 0 0
## 7.462687 0 1 0 2 0 0
## 8 0 1 0 0 0 0
## 8.955224 0 0 0 1 0 0
## 9.090909 0 0 0 1 0 0
## 12 0 0 0 1 0 0
## 27.272727 0 1 0 0 0 0
## 28 0 1 0 0 0 0
## 30.30303 0 0 1 0 0 0
punto 2.3 vi
table(UScereal$sodium,UScereal$sugars)
##
## 0 0.8 1.769912 2 3 4 4.477612 5.681818 6 6.666667 7.462687 8.270677
## 0 3 0 0 0 0 0 0 0 0 0 0 0
## 51.13636 0 0 0 0 0 0 0 0 0 0 0 0
## 90 0 0 0 0 0 0 0 0 0 0 0 0
## 93.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 125 0 0 0 0 0 0 0 0 0 0 0 0
## 135.33835 0 0 0 0 0 0 0 0 0 0 0 1
## 140 0 0 0 0 0 0 0 0 0 0 0 0
## 159.09091 0 0 0 0 0 0 0 1 0 0 0 0
## 173.33333 0 0 0 1 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 0 0 0 0 0 0 0
## 186.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 190 0 0 0 0 0 0 0 0 0 0 0 0
## 200 0 0 0 0 3 0 0 0 0 0 0 0
## 212.38938 0 0 1 0 0 0 0 0 0 0 0 0
## 220 0 0 0 0 1 0 0 0 1 0 0 0
## 223.8806 0 0 0 0 0 0 0 0 0 0 0 0
## 226.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 227.27273 0 0 0 0 0 0 0 0 0 0 0 0
## 230 0 0 0 0 1 0 0 0 0 0 0 0
## 232 0 1 0 0 0 0 0 0 0 0 0 0
## 238.80597 0 0 0 0 0 0 0 0 0 0 0 0
## 240 0 0 0 0 0 0 0 0 0 0 0 0
## 253.33333 0 0 0 0 0 0 0 0 0 1 0 0
## 266.66667 0 0 0 0 0 0 0 0 0 0 0 0
## 270 0 0 0 0 0 0 0 0 0 0 0 0
## 280 1 0 0 0 1 0 0 0 0 0 0 0
## 283.58209 0 0 0 0 0 0 0 0 0 0 0 0
## 290 0 0 0 1 1 0 0 0 0 0 0 0
## 293.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 298.50746 0 0 0 0 0 0 0 0 0 0 0 0
## 313.43284 0 0 0 0 0 0 0 0 0 0 1 0
## 320 0 0 0 0 1 0 0 0 0 0 0 0
## 328.35821 0 0 0 0 0 0 0 0 0 0 0 0
## 333.33333 0 0 0 0 0 1 0 0 0 0 0 0
## 340 0 0 0 0 0 0 0 0 0 0 0 0
## 343.28358 0 0 0 0 0 0 1 0 0 0 0 0
## 358.20896 0 0 0 0 0 0 0 0 0 0 0 0
## 373.33333 0 0 0 0 0 0 0 0 0 0 0 0
## 393.93939 0 0 0 0 0 0 0 0 0 0 0 0
## 680 0 0 0 0 0 0 0 0 0 0 0 0
## 787.87879 0 0 0 0 0 0 0 0 0 0 0 0
##
## 8.75 8.955224 10.447761 10.666667 11 12 12.121212 13 13.333333
## 0 1 0 0 0 0 1 0 0 0
## 51.13636 0 0 0 0 0 0 0 0 0
## 90 0 0 0 0 0 1 0 0 0
## 93.33333 0 0 0 0 0 0 0 0 0
## 125 0 0 0 0 0 0 0 1 0
## 135.33835 0 0 0 0 0 0 0 0 0
## 140 0 0 0 0 0 1 0 0 0
## 159.09091 0 0 0 0 0 0 0 0 0
## 173.33333 0 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 1 0 2 0
## 186.66667 0 0 0 0 0 0 0 0 1
## 190 0 0 0 0 0 0 0 0 0
## 200 0 0 0 0 0 0 0 0 0
## 212.38938 0 0 0 0 0 0 0 0 0
## 220 0 0 0 0 1 0 0 0 0
## 223.8806 0 1 0 0 0 0 0 0 0
## 226.66667 0 0 0 0 0 1 0 0 0
## 227.27273 0 0 0 0 0 0 1 0 0
## 230 0 0 0 0 0 0 0 0 0
## 232 0 0 0 0 0 0 0 0 0
## 238.80597 0 0 0 0 0 0 0 0 0
## 240 0 0 0 0 0 0 0 0 1
## 253.33333 0 0 0 0 0 0 0 0 0
## 266.66667 0 0 0 1 0 0 0 0 0
## 270 0 0 0 0 0 1 0 0 0
## 280 0 0 0 1 0 1 0 0 0
## 283.58209 0 0 0 0 0 0 0 0 0
## 290 0 0 0 0 0 0 0 0 0
## 293.33333 0 0 0 0 0 0 0 0 0
## 298.50746 0 1 0 0 0 0 0 0 0
## 313.43284 0 0 0 0 0 0 0 0 0
## 320 0 0 0 0 0 0 0 0 0
## 328.35821 0 0 1 0 0 0 0 0 0
## 333.33333 0 0 0 0 0 0 0 0 1
## 340 0 0 0 0 0 0 0 0 0
## 343.28358 0 0 0 0 0 0 0 0 0
## 358.20896 0 0 0 0 0 0 0 0 0
## 373.33333 0 0 0 0 0 1 0 0 0
## 393.93939 0 0 0 0 0 0 0 0 0
## 680 0 0 0 0 0 1 0 0 0
## 787.87879 0 0 0 0 0 0 0 0 0
##
## 13.432836 14 14.666667 14.925373 15.151515 16 17.045455 17.910448
## 0 0 0 0 0 0 0 0 0
## 51.13636 0 0 0 0 0 0 1 0
## 90 0 0 0 0 0 0 0 0
## 93.33333 0 0 0 0 0 0 0 0
## 125 0 1 0 0 0 0 0 0
## 135.33835 0 0 0 0 0 0 0 0
## 140 0 0 0 0 0 0 0 0
## 159.09091 0 0 0 0 0 0 0 0
## 173.33333 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 1 0 0
## 186.66667 0 0 0 0 0 0 0 0
## 190 0 1 0 0 0 0 0 0
## 200 0 0 0 0 0 0 0 0
## 212.38938 0 0 0 0 0 0 0 0
## 220 0 0 0 0 0 0 0 0
## 223.8806 0 0 0 0 0 0 0 0
## 226.66667 0 0 0 0 0 0 0 0
## 227.27273 0 0 0 0 0 0 0 0
## 230 0 0 0 0 0 0 0 0
## 232 0 0 0 0 0 0 0 0
## 238.80597 0 0 0 1 0 0 0 0
## 240 0 0 0 0 0 0 0 0
## 253.33333 0 0 0 0 0 0 0 0
## 266.66667 0 0 1 0 0 0 0 0
## 270 0 0 0 0 0 0 0 0
## 280 0 2 0 0 0 2 0 0
## 283.58209 1 0 0 0 0 0 0 0
## 290 0 0 0 0 0 0 0 0
## 293.33333 0 0 0 0 0 1 0 0
## 298.50746 0 0 0 0 0 0 0 0
## 313.43284 0 0 0 0 0 0 0 0
## 320 0 0 0 0 0 0 0 0
## 328.35821 0 0 0 0 0 0 0 0
## 333.33333 0 0 0 0 0 0 0 0
## 340 0 0 0 0 0 0 0 0
## 343.28358 0 0 0 0 0 0 0 0
## 358.20896 0 0 0 0 0 0 0 1
## 373.33333 0 0 0 0 0 0 0 0
## 393.93939 0 0 0 0 0 0 0 0
## 680 0 0 0 0 0 0 0 0
## 787.87879 0 0 0 0 1 0 0 0
##
## 18.181818 19.402985 20 20.895522
## 0 0 0 0 0
## 51.13636 0 0 0 0
## 90 0 0 0 0
## 93.33333 0 0 1 0
## 125 0 0 0 0
## 135.33835 0 0 0 0
## 140 0 0 0 0
## 159.09091 0 0 0 0
## 173.33333 0 0 0 0
## 180 0 0 0 0
## 186.66667 0 0 0 0
## 190 0 0 0 0
## 200 0 0 0 0
## 212.38938 0 0 0 0
## 220 0 0 0 0
## 223.8806 0 1 0 0
## 226.66667 0 0 0 0
## 227.27273 0 0 0 0
## 230 0 0 0 0
## 232 0 0 0 0
## 238.80597 0 0 0 0
## 240 0 0 0 0
## 253.33333 0 0 0 0
## 266.66667 0 0 0 0
## 270 0 0 0 0
## 280 0 0 0 0
## 283.58209 0 0 0 0
## 290 0 0 0 0
## 293.33333 0 0 0 0
## 298.50746 0 0 0 1
## 313.43284 0 0 0 0
## 320 0 0 0 0
## 328.35821 0 0 0 0
## 333.33333 0 0 0 0
## 340 0 0 1 0
## 343.28358 0 0 0 0
## 358.20896 0 0 0 0
## 373.33333 0 0 0 0
## 393.93939 1 0 0 0
## 680 0 0 0 0
## 787.87879 0 0 0 0
punto 2.4 a
cor(mammals$body, mammals$brain)
## [1] 0.9341638
punto 2.4 b
plot(mammals$body, mammals$brain)
punto 2.4 c
log_body <- log(mammals$body)
log_brain <- log(mammals$brain)
plot(log_body, log_brain)
cor(log_body, log_brain)
## [1] 0.9595748
punto 2.5 a
cor(emissions[,c("GDP", "perCapita", "CO2")])
## GDP perCapita CO2
## GDP 1.0000000 0.4325303 0.9501753
## perCapita 0.4325303 1.0000000 0.2757962
## CO2 0.9501753 0.2757962 1.0000000
punto 2.5 b
model <- lm(CO2 ~ GDP + perCapita, data = emissions)
summary(model)
##
## Call:
## lm(formula = CO2 ~ GDP + perCapita, data = emissions)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1037.3 -167.4 10.8 153.2 1052.0
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.100e+02 2.044e+02 2.495 0.0202 *
## GDP 8.406e-04 5.198e-05 16.172 4.68e-14 ***
## perCapita -3.039e-02 1.155e-02 -2.631 0.0149 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 382.8 on 23 degrees of freedom
## Multiple R-squared: 0.9253, Adjusted R-squared: 0.9188
## F-statistic: 142.5 on 2 and 23 DF, p-value: 1.102e-13
punto 2.5 c
cooksd <- cooks.distance(model)
plot(cooksd, type="h")
limit <- 4/((nrow(emissions)-length(coef(model))-2))
outliers <- which(cooksd > limit)
new_model <- update(model, subset = -outliers)
summary(new_model)
##
## Call:
## lm(formula = CO2 ~ GDP + perCapita, data = emissions, subset = -outliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -357.01 -72.86 -17.64 44.02 519.82
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.056e+02 1.123e+02 1.831 0.0821 .
## GDP 7.151e-04 8.823e-05 8.106 9.51e-08 ***
## perCapita -1.083e-02 6.385e-03 -1.697 0.1053
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 189.5 on 20 degrees of freedom
## Multiple R-squared: 0.7699, Adjusted R-squared: 0.7469
## F-statistic: 33.45 on 2 and 20 DF, p-value: 4.166e-07
punto 2.6 a
tapply(anorexia$Prewt,anorexia$Treat,mean)
## CBT Cont FT
## 82.68966 81.55769 83.22941
punto 2.6 b
sum(anorexia$Postwt > 0) # Pacientes que ganaron peso
## [1] 72
sum(anorexia$Postwt < 0) # Pacientes que perdieron peso
## [1] 0
punto 2.7 a y b
#punto a
set.seed(123) # Para reproducibilidad
vector1 <- rnorm(50)
vector2 <- rnorm(50)
shapiro.test(vector1)
##
## Shapiro-Wilk normality test
##
## data: vector1
## W = 0.98928, p-value = 0.9279
shapiro.test(vector2)
##
## Shapiro-Wilk normality test
##
## data: vector2
## W = 0.99073, p-value = 0.9618
#punto b
t.test(vector1, vector2)
##
## Welch Two Sample t-test
##
## data: vector1 and vector2
## t = -0.61157, df = 97.951, p-value = 0.5422
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.4754485 0.2514391
## sample estimates:
## mean of x mean of y
## 0.03440355 0.14640827
punto 2.8
#punto a
vector_normal <- rnorm(50)
vector_binomial <- rbinom(50, size=10, prob=0.5)
shapiro.test(vector_normal)
##
## Shapiro-Wilk normality test
##
## data: vector_normal
## W = 0.97211, p-value = 0.2814
shapiro.test(vector_binomial)
##
## Shapiro-Wilk normality test
##
## data: vector_binomial
## W = 0.96424, p-value = 0.1338
#punto b
t.test(vector_normal, vector_binomial)
##
## Welch Two Sample t-test
##
## data: vector_normal and vector_binomial
## t = -18.185, df = 76.677, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -5.807036 -4.660765
## sample estimates:
## mean of x mean of y
## -0.2539004 4.9800000
punto 2.9
#punto a
values <- rnorm(100)
hist(values)
#punto b
set.seed(123)
par(mfrow=c(2,2)) # Configurar la disposición de los gráficos
for(i in 1:4) {
values <- rnorm(100)
hist(values, main=paste("Histogram", i))}
#punto c
summary(values)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -2.465898 -0.729376 -0.003509 -0.036223 0.688690 2.571458
punto 2.10 (instalar libreria) primera parte
install.packages("pwr")
## Installing package into 'C:/Users/GuillermoA/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'pwr' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\GuillermoA\AppData\Local\Temp\Rtmpw7iljs\downloaded_packages
library(pwr)
punto 2.10 segunda parte
# Ejemplo práctico de la función pwr.t.test
# Ejemplo 1: Calcular tamaño de muestra necesario para detectar una diferencia
pwr.t.test(d=0.5, power=0.8, sig.level=0.05, type="two.sample", alternative="two.sided")
##
## Two-sample t test power calculation
##
## n = 63.76561
## d = 0.5
## sig.level = 0.05
## power = 0.8
## alternative = two.sided
##
## NOTE: n is number in *each* group
punto 2.10 tercera parte
# Ejemplo 2: Calcular el poder del test dado un tamaño de muestra
pwr.t.test(n=50, d=0.5, sig.level=0.05, type="two.sample", alternative="two.sided")
##
## Two-sample t test power calculation
##
## n = 50
## d = 0.5
## sig.level = 0.05
## power = 0.6968934
## alternative = two.sided
##
## NOTE: n is number in *each* group