# 2.1.a

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
#2.1.B
data(package = .packages(all.available = TRUE))
data(bumpers)
hist(bumpers)

data("firstchi")
boxplot(firstchi)

data(math)
boxplot(math)

#2.1.C
median(bumpers)
## [1] 2129
## [1] 2129
mean(bumpers)
## [1] 2122.478
## [1] 2122.478
sd(bumpers)
## [1] 798.4574
## [1] 798.4574
hist(bumpers)

median(firstchi)
## [1] 23
## [1] 23
mean(firstchi)
## [1] 23.97701
## [1] 23.97701
sd(firstchi)
## [1] 6.254258
## [1] 6.254258
hist(firstchi)

median(math)
## [1] 54
## [1] 54
mean(math)
## [1] 54.9
## [1] 54.9
sd(math)
## [1] 9.746264
## [1] 9.746264
hist(math)

#2.2.a
hist(brightness, probability = TRUE)
lines(density(brightness), col="red",lwd=3)

#2.2.b
boxplot(brightness)

data("brightness")
hist(brightness)

min(brightness)
## [1] 2.07
## [1] 2.07
min(brightness[brightness > min(brightness)])
## [1] 2.28
## [1] 2.28
#2.2.c
boxplot(brightness)

quantile(brightness)
##      0%     25%     50%     75%    100% 
##  2.0700  7.7025  8.5000  9.1300 12.4300
##      0%     25%     50%     75%    100% 
##  2.0700  7.7025  8.5000  9.1300 12.4300
brightness.sin <- brightness[brightness > 7.702 & brightness < 9.130]
boxplot(brightness.sin)

library(MASS)
#2.3.a
data("UScereal")
str(UScereal)
## 'data.frame':    65 obs. of  11 variables:
##  $ mfr      : Factor w/ 6 levels "G","K","N","P",..: 3 2 2 1 2 1 6 4 5 1 ...
##  $ calories : num  212 212 100 147 110 ...
##  $ protein  : num  12.12 12.12 8 2.67 2 ...
##  $ fat      : num  3.03 3.03 0 2.67 0 ...
##  $ sodium   : num  394 788 280 240 125 ...
##  $ fibre    : num  30.3 27.3 28 2 1 ...
##  $ carbo    : num  15.2 21.2 16 14 11 ...
##  $ sugars   : num  18.2 15.2 0 13.3 14 ...
##  $ shelf    : int  3 3 3 1 2 3 1 3 2 1 ...
##  $ potassium: num  848.5 969.7 660 93.3 30 ...
##  $ vitamins : Factor w/ 3 levels "100%","enriched",..: 2 2 2 2 2 2 2 2 2 2 ...
#2.3.B
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
#2.3.B
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
table(UScereal$vitamins,UScereal$fat)
##           
##             0 0.6666667  1 1.1363636 1.3333333 1.4925373 1.6  2 2.6666667
##   100%      1         0  3         0         1         0   0  0         0
##   enriched 18         1  7         1         8         4   1  2         3
##   none      3         0  0         0         0         0   0  0         0
##           
##            2.9850746 3.030303  4  6 9.0909091
##   100%             0        0  0  0         0
##   enriched         4        2  4  1         1
##   none             0        0  0  0         0
table(UScereal$shelf,UScereal$fat)
##    
##      0 0.6666667  1 1.1363636 1.3333333 1.4925373 1.6  2 2.6666667 2.9850746
##   1 10         0  2         0         2         2   1  0         1         0
##   2  3         1  5         0         4         1   0  1         1         1
##   3  9         0  3         1         3         1   0  1         1         3
##    
##     3.030303  4  6 9.0909091
##   1        0  0  0         0
##   2        0  1  0         0
##   3        2  3  1         1
table(UScereal$shelf,UScereal$fat)
##    
##      0 0.6666667  1 1.1363636 1.3333333 1.4925373 1.6  2 2.6666667 2.9850746
##   1 10         0  2         0         2         2   1  0         1         0
##   2  3         1  5         0         4         1   0  1         1         1
##   3  9         0  3         1         3         1   0  1         1         3
##    
##     3.030303  4  6 9.0909091
##   1        0  0  0         0
##   2        0  1  0         0
##   3        2  3  1         1
table(UScereal$mfr, UScereal$fibre)
##    
##     0 1 1.333333 1.6 2 2.666667 2.985075 3 3.409091 3.75 4 4.477612 5 5.970149
##   G 9 0        1   1 3        2        0 3        0    0 2        0 1        0
##   K 2 7        2   0 0        1        0 0        0    1 1        2 0        0
##   N 0 0        0   0 0        0        0 0        0    0 0        1 0        1
##   P 3 0        0   0 0        0        0 0        1    0 0        0 0        0
##   Q 2 1        0   0 0        0        1 0        0    0 1        0 0        0
##   R 2 0        1   0 0        0        0 0        0    0 0        1 0        1
##    
##     6.666667 7.462687 8 8.955224 9.090909 12 27.272727 28 30.30303
##   G        0        0 0        0        0  0         0  0        0
##   K        1        1 1        0        0  0         1  1        0
##   N        0        0 0        0        0  0         0  0        1
##   P        0        2 0        1        1  1         0  0        0
##   Q        0        0 0        0        0  0         0  0        0
##   R        0        0 0        0        0  0         0  0        0
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
#2.4
cor(mammals$body, mammals$brain)
## [1] 0.9341638
## [1] 0.9341638
plot(mammals, main="Datos")

plot(log(mammals), main="Resultado")

#2.5
cor(emissions)
##                 GDP perCapita       CO2
## GDP       1.0000000 0.4325303 0.9501753
## perCapita 0.4325303 1.0000000 0.2757962
## CO2       0.9501753 0.2757962 1.0000000
pairs(emissions)

r_lineal= lm(emissions$CO2 ~ emissions$GDP + emissions$perCapita, data = emissions)
summary(r_lineal)
## 
## Call:
## lm(formula = emissions$CO2 ~ emissions$GDP + emissions$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 *  
## emissions$GDP        8.406e-04  5.198e-05  16.172 4.68e-14 ***
## emissions$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
plot(emissions$GDP+emissions$perCapita,emissions$CO2)
abline(r_lineal,col = "red")
## Warning in abline(r_lineal, col = "red"): only using the first two of 3
## regression coefficients

emissions$CO2
##  [1] 6750 1320 1740  550  675  540 2000  700  370  480  240  400  145   75   80
## [16]   54   75  125  420   75   56  160  150   76   85   63
CO2_predict <- predict(r_lineal,emissions)
plot(emissions$GDP+emissions$perCapita,CO2_predict)

CO2_predict
##  UnitedStates         Japan       Germany        France UnitedKingdom 
##   6403.720110   2357.274571   1328.457202    939.412260    915.510264 
##         Italy        Russia        Canada         Spain     Australia 
##    888.117914    948.030553    418.174003    551.546727    203.695487 
##   Netherlands        Poland       Belgium        Sweden       Austria 
##    137.905405    524.997147      3.295727     57.168681      6.260516 
##   Switzerland      Portugal        Greece       Ukraine       Denmark 
##    -65.251059    177.533136    235.468677    538.782254    -82.034073 
##        Norway       Romania CzechRepublic       Finland       Hungary 
##   -213.820805    449.888660    273.235200     -5.729292    353.120804 
##       Ireland 
##     59.239930
cor(emissions$CO2,CO2_predict)
## [1] 0.9619321
#2.6
data("anorexia")
head(anorexia)
##   Treat Prewt Postwt
## 1  Cont  80.7   80.2
## 2  Cont  89.4   80.1
## 3  Cont  91.8   86.4
## 4  Cont  74.0   86.3
## 5  Cont  78.1   76.1
## 6  Cont  88.3   78.1
hist(anorexia)

pos_pesos <- which(anorexia$Postwt>anorexia$Prewt)
casos_exito = anorexia[c(pos_pesos),]
mejor_tratamiento = which.max(table(casos_exito$Treat))
mejor_tratamiento = c(paste(names(mejor_tratamiento),": ",max(table(casos_exito$Treat))," Casos positivos logrando el mejor tratamiento"))
table(casos_exito$Treat)
## 
##  CBT Cont   FT 
##   18   11   13
mejor_tratamiento
## [1] "CBT :  18  Casos positivos logrando el mejor tratamiento"
#B
pos_perdida_peso <- which(anorexia$Postwt<anorexia$Prewt)
casos_fracaso = anorexia[c(pos_perdida_peso),]
ganaron_peso = length(pos_pesos)
ganaron_peso
## [1] 42
perdieron_peso = nrow(casos_fracaso)
perdieron_peso
## [1] 29
#C
barplot(c(ganaron_peso,perdieron_peso), main = "Pacientes que ganaron y que perdieron peso",
        ylab = "Cantidad de pacientes", col = c("red","green"))
legend("topright", legend = c(paste("Ganaron peso: ",ganaron_peso),paste("Perdieron peso: ",perdieron_peso)),
       fill = c("red","green"))

#2.7
#a
fallecidos = nrow(Melanoma[Melanoma$status==1,]) + nrow(Melanoma[Melanoma$status==3,])
fallecidos
## [1] 71
barplot(table(Melanoma$status), main = "Presencia y ausencia de Melanoma en una base de datos de 205 personas",
        ylab = "Unidad por persona", col = c("red","blue","green"))
legend("topright",legend = c(paste("MuertexMelanoma:", nrow(Melanoma[Melanoma$status==1,])),paste("Vivo:",nrow(Melanoma[Melanoma$status==2,])),paste("MuertexOtras causas",nrow(Melanoma[Melanoma$status==3,]))),
       fill = c("red","blue","green"))

#C
Melanoma1 = Melanoma
s1 <- which((Melanoma1$status==1))
s2 <- which((Melanoma1$status==2))
s3 <- which((Melanoma1$status==3))
Melanoma1$status = replace(Melanoma1$status,s3,1)
Melanoma1$status = replace(Melanoma1$status,s2,0)
c(paste("tumor/muerte: ",cor(Melanoma1$thickness,Melanoma1$status),3))
## [1] "tumor/muerte:  0.314179811783222 3"
#D
remplazo = replace(Melanoma1$status,Melanoma1$status==0,"Vivos")
remplazo = replace(remplazo,Melanoma1$status==1,"Fallecidos")
boxplot(Melanoma1$thickness~remplazo, main = "Relación entre tamaño de tumor y muerte",
        ylab = "Tamaño del tumor (mm)",xlab = "Estado del paciente")

#2.8
#a
table(babyboom$gender)
## 
## girl  boy 
##   18   26
#b
sum(with(babyboom, clock.time <= 1200))
## [1] 18
#C
pesoNiños=sum(with(babyboom,gender == "boy" & wt<3000))
pesoNiños
## [1] 4
#D
barplot(table(babyboom$gender,babyboom$wt<3000),beside = T,col = c("red","green"),xlab="Género",ylab="cantidad") 

#e
p_ninos = median(babyboom$wt[babyboom$gender=='boy'])
p_ninas = median(babyboom$wt[babyboom$gender=='girl'])

barplot(c(ganaron_peso,perdieron_peso), main = "Pacientes que ganaron y que perdieron peso",
        ylab = "Cantidad de pacientes", col = c("darkorange","skyblue"))
legend("topright", legend = c(paste("Ganaron peso: ",p_ninos),paste("Perdieron peso: ",p_ninas)),
       fill = c("darkorange","skyblue"))

#2.9
#a
aggregate(Aids2$state, by=list(Aids2[,"state"]), FUN=length)
##   Group.1    x
## 1     NSW 1780
## 2   Other  249
## 3     QLD  226
## 4     VIC  588
table(Aids2$status=='D')
## 
## FALSE  TRUE 
##  1082  1761
table(Aids2$sex, Aids2$T.categ)
##    
##       hs hsid   id  het haem blood mother other
##   F    1    0   20   20    0    37      4     7
##   M 2464   72   28   21   46    57      3    63
colores = c("Red","skyblue","blueviolet","tomato","darkgreen","thistle","darkorange","gold")
pie(table(Aids2$T.categ),col = colores,labels = table(Aids2$T.categ),
    main = "Cantidad y tipos de transmisión")
legend("topright",legend = levels(Aids2$T.categ),fill = colores)

#2.10
#A

if(mean(crime$y1993) > mean(crime$y1983)){
  print("FUE MAYOR 1993")
}else{
  print("NO FUE MAYOR 1993 a 1983")
}
## [1] "FUE MAYOR 1993"
#B
crime[crime$y1993 == max(crime$y1993),]
##     y1983  y1993
## DC 1985.4 2832.8