R Markdown
This is an R Markdown document. Markdown is a simple formatting
syntax for authoring HTML, PDF, and MS Word documents. For more details
on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be
generated that includes both content as well as the output of any
embedded R code chunks within the document. You can embed an R code
chunk like this:
#EJERCICIO_1
library(datasets)
library(knitr)
data(iris)
data(Orange)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 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.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
head(Orange)
## Tree age circumference
## 1 1 118 30
## 2 1 484 58
## 3 1 664 87
## 4 1 1004 115
## 5 1 1231 120
## 6 1 1372 142
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
summary(Orange)
## Tree age circumference
## 3:7 Min. : 118.0 Min. : 30.0
## 1:7 1st Qu.: 484.0 1st Qu.: 65.5
## 5:7 Median :1004.0 Median :115.0
## 2:7 Mean : 922.1 Mean :115.9
## 4:7 3rd Qu.:1372.0 3rd Qu.:161.5
## Max. :1582.0 Max. :214.0
kable(table(iris$Sepal.Length))
| 4.3 |
1 |
| 4.4 |
3 |
| 4.5 |
1 |
| 4.6 |
4 |
| 4.7 |
2 |
| 4.8 |
5 |
| 4.9 |
6 |
| 5 |
10 |
| 5.1 |
9 |
| 5.2 |
4 |
| 5.3 |
1 |
| 5.4 |
6 |
| 5.5 |
7 |
| 5.6 |
6 |
| 5.7 |
8 |
| 5.8 |
7 |
| 5.9 |
3 |
| 6 |
6 |
| 6.1 |
6 |
| 6.2 |
4 |
| 6.3 |
9 |
| 6.4 |
7 |
| 6.5 |
5 |
| 6.6 |
2 |
| 6.7 |
8 |
| 6.8 |
3 |
| 6.9 |
4 |
| 7 |
1 |
| 7.1 |
1 |
| 7.2 |
3 |
| 7.3 |
1 |
| 7.4 |
1 |
| 7.6 |
1 |
| 7.7 |
4 |
| 7.9 |
1 |
kable(table(iris$Sepal.Width))
| 2 |
1 |
| 2.2 |
3 |
| 2.3 |
4 |
| 2.4 |
3 |
| 2.5 |
8 |
| 2.6 |
5 |
| 2.7 |
9 |
| 2.8 |
14 |
| 2.9 |
10 |
| 3 |
26 |
| 3.1 |
11 |
| 3.2 |
13 |
| 3.3 |
6 |
| 3.4 |
12 |
| 3.5 |
6 |
| 3.6 |
4 |
| 3.7 |
3 |
| 3.8 |
6 |
| 3.9 |
2 |
| 4 |
1 |
| 4.1 |
1 |
| 4.2 |
1 |
| 4.4 |
1 |
kable(table(iris$Petal.Length))
| 1 |
1 |
| 1.1 |
1 |
| 1.2 |
2 |
| 1.3 |
7 |
| 1.4 |
13 |
| 1.5 |
13 |
| 1.6 |
7 |
| 1.7 |
4 |
| 1.9 |
2 |
| 3 |
1 |
| 3.3 |
2 |
| 3.5 |
2 |
| 3.6 |
1 |
| 3.7 |
1 |
| 3.8 |
1 |
| 3.9 |
3 |
| 4 |
5 |
| 4.1 |
3 |
| 4.2 |
4 |
| 4.3 |
2 |
| 4.4 |
4 |
| 4.5 |
8 |
| 4.6 |
3 |
| 4.7 |
5 |
| 4.8 |
4 |
| 4.9 |
5 |
| 5 |
4 |
| 5.1 |
8 |
| 5.2 |
2 |
| 5.3 |
2 |
| 5.4 |
2 |
| 5.5 |
3 |
| 5.6 |
6 |
| 5.7 |
3 |
| 5.8 |
3 |
| 5.9 |
2 |
| 6 |
2 |
| 6.1 |
3 |
| 6.3 |
1 |
| 6.4 |
1 |
| 6.6 |
1 |
| 6.7 |
2 |
| 6.9 |
1 |
kable(table(iris$Petal.Width))
| 0.1 |
5 |
| 0.2 |
29 |
| 0.3 |
7 |
| 0.4 |
7 |
| 0.5 |
1 |
| 0.6 |
1 |
| 1 |
7 |
| 1.1 |
3 |
| 1.2 |
5 |
| 1.3 |
13 |
| 1.4 |
8 |
| 1.5 |
12 |
| 1.6 |
4 |
| 1.7 |
2 |
| 1.8 |
12 |
| 1.9 |
5 |
| 2 |
6 |
| 2.1 |
6 |
| 2.2 |
3 |
| 2.3 |
8 |
| 2.4 |
3 |
| 2.5 |
3 |
kable(table(iris$Species))
| setosa |
50 |
| versicolor |
50 |
| virginica |
50 |
kable(prop.table(iris$Sepal.Length))
| 0.0058186 |
| 0.0055904 |
| 0.0053622 |
| 0.0052481 |
| 0.0057045 |
| 0.0061609 |
| 0.0052481 |
| 0.0057045 |
| 0.0050200 |
| 0.0055904 |
| 0.0061609 |
| 0.0054763 |
| 0.0054763 |
| 0.0049059 |
| 0.0066172 |
| 0.0065031 |
| 0.0061609 |
| 0.0058186 |
| 0.0065031 |
| 0.0058186 |
| 0.0061609 |
| 0.0058186 |
| 0.0052481 |
| 0.0058186 |
| 0.0054763 |
| 0.0057045 |
| 0.0057045 |
| 0.0059327 |
| 0.0059327 |
| 0.0053622 |
| 0.0054763 |
| 0.0061609 |
| 0.0059327 |
| 0.0062750 |
| 0.0055904 |
| 0.0057045 |
| 0.0062750 |
| 0.0055904 |
| 0.0050200 |
| 0.0058186 |
| 0.0057045 |
| 0.0051341 |
| 0.0050200 |
| 0.0057045 |
| 0.0058186 |
| 0.0054763 |
| 0.0058186 |
| 0.0052481 |
| 0.0060468 |
| 0.0057045 |
| 0.0079863 |
| 0.0073018 |
| 0.0078722 |
| 0.0062750 |
| 0.0074159 |
| 0.0065031 |
| 0.0071877 |
| 0.0055904 |
| 0.0075299 |
| 0.0059327 |
| 0.0057045 |
| 0.0067313 |
| 0.0068454 |
| 0.0069595 |
| 0.0063890 |
| 0.0076440 |
| 0.0063890 |
| 0.0066172 |
| 0.0070736 |
| 0.0063890 |
| 0.0067313 |
| 0.0069595 |
| 0.0071877 |
| 0.0069595 |
| 0.0073018 |
| 0.0075299 |
| 0.0077581 |
| 0.0076440 |
| 0.0068454 |
| 0.0065031 |
| 0.0062750 |
| 0.0062750 |
| 0.0066172 |
| 0.0068454 |
| 0.0061609 |
| 0.0068454 |
| 0.0076440 |
| 0.0071877 |
| 0.0063890 |
| 0.0062750 |
| 0.0062750 |
| 0.0069595 |
| 0.0066172 |
| 0.0057045 |
| 0.0063890 |
| 0.0065031 |
| 0.0065031 |
| 0.0070736 |
| 0.0058186 |
| 0.0065031 |
| 0.0071877 |
| 0.0066172 |
| 0.0081004 |
| 0.0071877 |
| 0.0074159 |
| 0.0086708 |
| 0.0055904 |
| 0.0083286 |
| 0.0076440 |
| 0.0082145 |
| 0.0074159 |
| 0.0073018 |
| 0.0077581 |
| 0.0065031 |
| 0.0066172 |
| 0.0073018 |
| 0.0074159 |
| 0.0087849 |
| 0.0087849 |
| 0.0068454 |
| 0.0078722 |
| 0.0063890 |
| 0.0087849 |
| 0.0071877 |
| 0.0076440 |
| 0.0082145 |
| 0.0070736 |
| 0.0069595 |
| 0.0073018 |
| 0.0082145 |
| 0.0084427 |
| 0.0090131 |
| 0.0073018 |
| 0.0071877 |
| 0.0069595 |
| 0.0087849 |
| 0.0071877 |
| 0.0073018 |
| 0.0068454 |
| 0.0078722 |
| 0.0076440 |
| 0.0078722 |
| 0.0066172 |
| 0.0077581 |
| 0.0076440 |
| 0.0076440 |
| 0.0071877 |
| 0.0074159 |
| 0.0070736 |
| 0.0067313 |
kable(prop.table(iris$Sepal.Width))
| 0.0076319 |
| 0.0065416 |
| 0.0069778 |
| 0.0067597 |
| 0.0078500 |
| 0.0085041 |
| 0.0074139 |
| 0.0074139 |
| 0.0063236 |
| 0.0067597 |
| 0.0080680 |
| 0.0074139 |
| 0.0065416 |
| 0.0065416 |
| 0.0087222 |
| 0.0095944 |
| 0.0085041 |
| 0.0076319 |
| 0.0082861 |
| 0.0082861 |
| 0.0074139 |
| 0.0080680 |
| 0.0078500 |
| 0.0071958 |
| 0.0074139 |
| 0.0065416 |
| 0.0074139 |
| 0.0076319 |
| 0.0074139 |
| 0.0069778 |
| 0.0067597 |
| 0.0074139 |
| 0.0089403 |
| 0.0091583 |
| 0.0067597 |
| 0.0069778 |
| 0.0076319 |
| 0.0078500 |
| 0.0065416 |
| 0.0074139 |
| 0.0076319 |
| 0.0050153 |
| 0.0069778 |
| 0.0076319 |
| 0.0082861 |
| 0.0065416 |
| 0.0082861 |
| 0.0069778 |
| 0.0080680 |
| 0.0071958 |
| 0.0069778 |
| 0.0069778 |
| 0.0067597 |
| 0.0050153 |
| 0.0061055 |
| 0.0061055 |
| 0.0071958 |
| 0.0052333 |
| 0.0063236 |
| 0.0058875 |
| 0.0043611 |
| 0.0065416 |
| 0.0047972 |
| 0.0063236 |
| 0.0063236 |
| 0.0067597 |
| 0.0065416 |
| 0.0058875 |
| 0.0047972 |
| 0.0054514 |
| 0.0069778 |
| 0.0061055 |
| 0.0054514 |
| 0.0061055 |
| 0.0063236 |
| 0.0065416 |
| 0.0061055 |
| 0.0065416 |
| 0.0063236 |
| 0.0056694 |
| 0.0052333 |
| 0.0052333 |
| 0.0058875 |
| 0.0058875 |
| 0.0065416 |
| 0.0074139 |
| 0.0067597 |
| 0.0050153 |
| 0.0065416 |
| 0.0054514 |
| 0.0056694 |
| 0.0065416 |
| 0.0056694 |
| 0.0050153 |
| 0.0058875 |
| 0.0065416 |
| 0.0063236 |
| 0.0063236 |
| 0.0054514 |
| 0.0061055 |
| 0.0071958 |
| 0.0058875 |
| 0.0065416 |
| 0.0063236 |
| 0.0065416 |
| 0.0065416 |
| 0.0054514 |
| 0.0063236 |
| 0.0054514 |
| 0.0078500 |
| 0.0069778 |
| 0.0058875 |
| 0.0065416 |
| 0.0054514 |
| 0.0061055 |
| 0.0069778 |
| 0.0065416 |
| 0.0082861 |
| 0.0056694 |
| 0.0047972 |
| 0.0069778 |
| 0.0061055 |
| 0.0061055 |
| 0.0058875 |
| 0.0071958 |
| 0.0069778 |
| 0.0061055 |
| 0.0065416 |
| 0.0061055 |
| 0.0065416 |
| 0.0061055 |
| 0.0082861 |
| 0.0061055 |
| 0.0061055 |
| 0.0056694 |
| 0.0065416 |
| 0.0074139 |
| 0.0067597 |
| 0.0065416 |
| 0.0067597 |
| 0.0067597 |
| 0.0067597 |
| 0.0058875 |
| 0.0069778 |
| 0.0071958 |
| 0.0065416 |
| 0.0054514 |
| 0.0065416 |
| 0.0074139 |
| 0.0065416 |
kable(prop.table(iris$Petal.Length))
| 0.0024836 |
| 0.0024836 |
| 0.0023062 |
| 0.0026610 |
| 0.0024836 |
| 0.0030158 |
| 0.0024836 |
| 0.0026610 |
| 0.0024836 |
| 0.0026610 |
| 0.0026610 |
| 0.0028384 |
| 0.0024836 |
| 0.0019514 |
| 0.0021288 |
| 0.0026610 |
| 0.0023062 |
| 0.0024836 |
| 0.0030158 |
| 0.0026610 |
| 0.0030158 |
| 0.0026610 |
| 0.0017740 |
| 0.0030158 |
| 0.0033706 |
| 0.0028384 |
| 0.0028384 |
| 0.0026610 |
| 0.0024836 |
| 0.0028384 |
| 0.0028384 |
| 0.0026610 |
| 0.0026610 |
| 0.0024836 |
| 0.0026610 |
| 0.0021288 |
| 0.0023062 |
| 0.0024836 |
| 0.0023062 |
| 0.0026610 |
| 0.0023062 |
| 0.0023062 |
| 0.0023062 |
| 0.0028384 |
| 0.0033706 |
| 0.0024836 |
| 0.0028384 |
| 0.0024836 |
| 0.0026610 |
| 0.0024836 |
| 0.0083378 |
| 0.0079830 |
| 0.0086926 |
| 0.0070960 |
| 0.0081604 |
| 0.0079830 |
| 0.0083378 |
| 0.0058542 |
| 0.0081604 |
| 0.0069186 |
| 0.0062090 |
| 0.0074508 |
| 0.0070960 |
| 0.0083378 |
| 0.0063864 |
| 0.0078056 |
| 0.0079830 |
| 0.0072734 |
| 0.0079830 |
| 0.0069186 |
| 0.0085152 |
| 0.0070960 |
| 0.0086926 |
| 0.0083378 |
| 0.0076282 |
| 0.0078056 |
| 0.0085152 |
| 0.0088700 |
| 0.0079830 |
| 0.0062090 |
| 0.0067412 |
| 0.0065638 |
| 0.0069186 |
| 0.0090474 |
| 0.0079830 |
| 0.0079830 |
| 0.0083378 |
| 0.0078056 |
| 0.0072734 |
| 0.0070960 |
| 0.0078056 |
| 0.0081604 |
| 0.0070960 |
| 0.0058542 |
| 0.0074508 |
| 0.0074508 |
| 0.0074508 |
| 0.0076282 |
| 0.0053220 |
| 0.0072734 |
| 0.0106440 |
| 0.0090474 |
| 0.0104666 |
| 0.0099344 |
| 0.0102892 |
| 0.0117084 |
| 0.0079830 |
| 0.0111762 |
| 0.0102892 |
| 0.0108214 |
| 0.0090474 |
| 0.0094022 |
| 0.0097570 |
| 0.0088700 |
| 0.0090474 |
| 0.0094022 |
| 0.0097570 |
| 0.0118858 |
| 0.0122406 |
| 0.0088700 |
| 0.0101118 |
| 0.0086926 |
| 0.0118858 |
| 0.0086926 |
| 0.0101118 |
| 0.0106440 |
| 0.0085152 |
| 0.0086926 |
| 0.0099344 |
| 0.0102892 |
| 0.0108214 |
| 0.0113536 |
| 0.0099344 |
| 0.0090474 |
| 0.0099344 |
| 0.0108214 |
| 0.0099344 |
| 0.0097570 |
| 0.0085152 |
| 0.0095796 |
| 0.0099344 |
| 0.0090474 |
| 0.0090474 |
| 0.0104666 |
| 0.0101118 |
| 0.0092248 |
| 0.0088700 |
| 0.0092248 |
| 0.0095796 |
| 0.0090474 |
kable(prop.table(iris$Petal.Width))
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0022235 |
| 0.0016676 |
| 0.0011117 |
| 0.0011117 |
| 0.0005559 |
| 0.0011117 |
| 0.0011117 |
| 0.0005559 |
| 0.0005559 |
| 0.0011117 |
| 0.0022235 |
| 0.0022235 |
| 0.0016676 |
| 0.0016676 |
| 0.0016676 |
| 0.0011117 |
| 0.0022235 |
| 0.0011117 |
| 0.0027793 |
| 0.0011117 |
| 0.0011117 |
| 0.0022235 |
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0022235 |
| 0.0005559 |
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0005559 |
| 0.0011117 |
| 0.0011117 |
| 0.0016676 |
| 0.0016676 |
| 0.0011117 |
| 0.0033352 |
| 0.0022235 |
| 0.0016676 |
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0011117 |
| 0.0077821 |
| 0.0083380 |
| 0.0083380 |
| 0.0072262 |
| 0.0083380 |
| 0.0072262 |
| 0.0088938 |
| 0.0055586 |
| 0.0072262 |
| 0.0077821 |
| 0.0055586 |
| 0.0083380 |
| 0.0055586 |
| 0.0077821 |
| 0.0072262 |
| 0.0077821 |
| 0.0083380 |
| 0.0055586 |
| 0.0083380 |
| 0.0061145 |
| 0.0100056 |
| 0.0072262 |
| 0.0083380 |
| 0.0066704 |
| 0.0072262 |
| 0.0077821 |
| 0.0077821 |
| 0.0094497 |
| 0.0083380 |
| 0.0055586 |
| 0.0061145 |
| 0.0055586 |
| 0.0066704 |
| 0.0088938 |
| 0.0083380 |
| 0.0088938 |
| 0.0083380 |
| 0.0072262 |
| 0.0072262 |
| 0.0072262 |
| 0.0066704 |
| 0.0077821 |
| 0.0066704 |
| 0.0055586 |
| 0.0072262 |
| 0.0066704 |
| 0.0072262 |
| 0.0072262 |
| 0.0061145 |
| 0.0072262 |
| 0.0138966 |
| 0.0105614 |
| 0.0116732 |
| 0.0100056 |
| 0.0122290 |
| 0.0116732 |
| 0.0094497 |
| 0.0100056 |
| 0.0100056 |
| 0.0138966 |
| 0.0111173 |
| 0.0105614 |
| 0.0116732 |
| 0.0111173 |
| 0.0133407 |
| 0.0127849 |
| 0.0100056 |
| 0.0122290 |
| 0.0127849 |
| 0.0083380 |
| 0.0127849 |
| 0.0111173 |
| 0.0111173 |
| 0.0100056 |
| 0.0116732 |
| 0.0100056 |
| 0.0100056 |
| 0.0100056 |
| 0.0116732 |
| 0.0088938 |
| 0.0105614 |
| 0.0111173 |
| 0.0122290 |
| 0.0083380 |
| 0.0077821 |
| 0.0127849 |
| 0.0133407 |
| 0.0100056 |
| 0.0100056 |
| 0.0116732 |
| 0.0133407 |
| 0.0127849 |
| 0.0105614 |
| 0.0127849 |
| 0.0138966 |
| 0.0127849 |
| 0.0105614 |
| 0.0111173 |
| 0.0127849 |
| 0.0100056 |
#prop.table(iris$Species) #Como son factores, no se puede generar la tabla
kable(table(Orange$Tree))
kable(table(Orange$age))
| 118 |
5 |
| 484 |
5 |
| 664 |
5 |
| 1004 |
5 |
| 1231 |
5 |
| 1372 |
5 |
| 1582 |
5 |
kable(table(Orange$circumference))
| 30 |
3 |
| 32 |
1 |
| 33 |
1 |
| 49 |
1 |
| 51 |
1 |
| 58 |
1 |
| 62 |
1 |
| 69 |
1 |
| 75 |
1 |
| 81 |
1 |
| 87 |
1 |
| 108 |
1 |
| 111 |
1 |
| 112 |
1 |
| 115 |
2 |
| 120 |
1 |
| 125 |
1 |
| 139 |
1 |
| 140 |
1 |
| 142 |
2 |
| 145 |
1 |
| 156 |
1 |
| 167 |
1 |
| 172 |
1 |
| 174 |
1 |
| 177 |
1 |
| 179 |
1 |
| 203 |
2 |
| 209 |
1 |
| 214 |
1 |
table(Orange$Tree, Orange$age)
##
## 118 484 664 1004 1231 1372 1582
## 3 1 1 1 1 1 1 1
## 1 1 1 1 1 1 1 1
## 5 1 1 1 1 1 1 1
## 2 1 1 1 1 1 1 1
## 4 1 1 1 1 1 1 1
#EJERCICIO_2
vect1 <- c(1,2,1,2,1,2,1,2,1,2,1,1,1,1,2,2,1,1,2,1)
vect2 <- c(1,1,2,2,2,1,2,1,1,2,1,2,1,1,1,2,1,1,1,1)
Bajo_Peso=factor(vect1, levels=c(1,2), labels=c("Bajo Peso","Peso Normal"))
Bajo_Peso
## [1] Bajo Peso Peso Normal Bajo Peso Peso Normal Bajo Peso Peso Normal
## [7] Bajo Peso Peso Normal Bajo Peso Peso Normal Bajo Peso Bajo Peso
## [13] Bajo Peso Bajo Peso Peso Normal Peso Normal Bajo Peso Bajo Peso
## [19] Peso Normal Bajo Peso
## Levels: Bajo Peso Peso Normal
Fumador=factor(vect2, levels=c(1,2), labels=c("Fuma", "No fuma"))
Fumador
## [1] Fuma Fuma No fuma No fuma No fuma Fuma No fuma Fuma Fuma
## [10] No fuma Fuma No fuma Fuma Fuma Fuma No fuma Fuma Fuma
## [19] Fuma Fuma
## Levels: Fuma No fuma
tabla<-table(Bajo_Peso, Fumador)
tabla
## Fumador
## Bajo_Peso Fuma No fuma
## Bajo Peso 8 4
## Peso Normal 5 3
chisq.test(tabla)
## Warning in chisq.test(tabla): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: tabla
## X-squared = 0, df = 1, p-value = 1
fisher.test(tabla)
##
## Fisher's Exact Test for Count Data
##
## data: tabla
## p-value = 1
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.1200513 10.9278345
## sample estimates:
## odds ratio
## 1.189031
#Según Pearson no hay evidencia de relación entre "fumar" y "peso". Sin embargo, nos sale una advertencia ya que este test no es fiable con muestras pequeñas, así que usamos el test de Fisher. Este test confirma el resultado anterior, no podemos rechazar la hipótesis nula de independencia entre fumar y peso ya que el p-valor=1. Además, el cociente de probabilidades (odds-ratio) es cercano a 1 (1.19), lo que demuesta que no hay prácticamente asociación.
#EJERCICIO_3
data("airquality")
data("airmiles")
par(mfrow=c(2,2)) #matriz 2x2
plot(airquality$Ozone, col="blue",pch="#") #apartado a)
boxplot(airquality$Temp, col="red", main="Temperatura (en grados Farenheit)")
plot(airmiles, main = "Datos de pasajeros en vuelos comerciales (miles)",
xlab = "Miles de pasajeros", col = "cadetblue2")
hist(airmiles, col="chocolate2", main="Histrograma de vuelos comerciales")

#EJERCICIO_4
set.seed(333)
x1 <- rnorm(500)
y1 <- x1 + rnorm(500)
plot(x1, y1, main="Gráfico de dispersión")
abline(lm(y1~x1), col="orange")
#EJERCICIO_5
library(ggplot2)
ggplot(airquality, aes(Solar.R, Temp)) +
geom_point(size=3) +
geom_smooth(method="lm", col="darkred")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 7 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).
ggplot (data=airquality, aes(x=Solar.R, y=Temp, col=factor(Month),
shape=factor(Month))) +
geom_point()
## Warning: Removed 7 rows containing missing values or values outside the scale range
## (`geom_point()`).
library (MASS)
ggplot(birthwt,aes(age)) +
geom_histogram(binwidth=5, fill="blue",col="black")
ggplot(birthwt,aes(age)) +
geom_histogram(binwidth=5, fill="violet",col="black")+
facet_grid(~smoke)
#EJERCICIO_6
jpeg("migrafic1.jpg") #abrimos el dispositivo gráfico
data(iris)
boxplot(iris$Petal.Width, col="green")
dev.off
## function (which = dev.cur())
## {
## if (which == 1)
## stop("cannot shut down device 1 (the null device)")
## .External(C_devoff, as.integer(which))
## dev.cur()
## }
## <bytecode: 0x144491cd8>
## <environment: namespace:grDevices>
pdf("migrafic2.pdf") #abrimos el dispositivo gráfico
data(iris)
boxplot(iris$Petal.Width, col="green")
dev.off #cerramos el dispositivo gráfico
## function (which = dev.cur())
## {
## if (which == 1)
## stop("cannot shut down device 1 (the null device)")
## .External(C_devoff, as.integer(which))
## dev.cur()
## }
## <bytecode: 0x144491cd8>
## <environment: namespace:grDevices>
#EJERCICIO_7
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::select() masks MASS::select()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data("Orange")
head(Orange)
## Grouped Data: circumference ~ age | Tree
## Tree age circumference
## 1 1 118 30
## 2 1 484 58
## 3 1 664 87
## 4 1 1004 115
## 5 1 1231 120
## 6 1 1372 142
summary(Orange)
## Tree age circumference
## 3:7 Min. : 118.0 Min. : 30.0
## 1:7 1st Qu.: 484.0 1st Qu.: 65.5
## 5:7 Median :1004.0 Median :115.0
## 2:7 Mean : 922.1 Mean :115.9
## 4:7 3rd Qu.:1372.0 3rd Qu.:161.5
## Max. :1582.0 Max. :214.0
pairs(Orange)

cor.test(Orange$age, Orange$circumference) #Hay correlación puesto que el valor es 0.91
##
## Pearson's product-moment correlation
##
## data: Orange$age and Orange$circumference
## t = 12.9, df = 33, p-value = 1.931e-14
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.8342364 0.9557955
## sample estimates:
## cor
## 0.9135189
mod_reg<-lm(circumference ~ age, data = Orange)
summary(mod_reg)
##
## Call:
## lm(formula = circumference ~ age, data = Orange)
##
## Residuals:
## Min 1Q Median 3Q Max
## -46.310 -14.946 -0.076 19.697 45.111
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 17.399650 8.622660 2.018 0.0518 .
## age 0.106770 0.008277 12.900 1.93e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 23.74 on 33 degrees of freedom
## Multiple R-squared: 0.8345, Adjusted R-squared: 0.8295
## F-statistic: 166.4 on 1 and 33 DF, p-value: 1.931e-14
plot(Orange$age,Orange$circumference)
abline(mod_reg)
abline(mod_reg, col = "orange")

días <- 600
medida <- 0.1068 * días + 17.3997
print(medida)
## [1] 81.4797
#EJERCICIO_8
Orange %>%
ggplot(aes(x = age,
y = circumference)) +
geom_point()

Orange %>%
ggplot(aes(x = age,
y = circumference)) +
geom_point() +
geom_abline(intercept = 17.3997,
slope = 0.1068,
col = 'blue') +
geom_vline(xintercept = 800,
col = 'red')

#EJERCICIO_9
data("PlantGrowth")
anova(lm(weight ~ group, data = PlantGrowth))
## Analysis of Variance Table
##
## Response: weight
## Df Sum Sq Mean Sq F value Pr(>F)
## group 2 3.7663 1.8832 4.8461 0.01591 *
## Residuals 27 10.4921 0.3886
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#La hipótesis nula del ANOVA es que todas las medias poblacionales de peso son iguales, pero la alternativa es que al menos unamedia es diferente. Como el p-value = 0.01591, rechazamos la hipótesis nula, ya que hay diferencias significativas entre al menos dos tratamientos respecto al peso de las plantas.Sin embargo, para aplicar un ANOVA correctamente tenemos que comprobar que se cumplan tres supuestos: 1)Independencia de las observaciones, 2) Normalidad de los residuos (distribución aproximadamente normal) que se puede comprobar con spahiro.test:
shapiro.test(residuals(lm(weight ~ group, data = PlantGrowth)))
##
## Shapiro-Wilk normality test
##
## data: residuals(lm(weight ~ group, data = PlantGrowth))
## W = 0.96607, p-value = 0.4379
qqnorm(residuals(lm(weight ~ group, data = PlantGrowth)))
qqline(residuals(lm(weight ~ group, data = PlantGrowth)))

#Y por último 3) Homogeneicdad de varianzas (Homocedaasticidad) que podemos comprobarlo así:
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
##
## The following object is masked from 'package:dplyr':
##
## recode
##
## The following object is masked from 'package:purrr':
##
## some
bartlett.test(weight ~ group, data = PlantGrowth) #En este caso no se rechaza la hipótesis nula, ya que no hay evidencia estadística de que las varianzas sean diferentes (p-value=0.2371 > 0.05)
##
## Bartlett test of homogeneity of variances
##
## data: weight by group
## Bartlett's K-squared = 2.8786, df = 2, p-value = 0.2371
#Si estas condiciones no se cumplen (no normalidad o no homocedasticidad) hay que utilizar una alternativa no paramétrica como el test de Kruskal-Wallis que compara medianas en vez de medias. Si la ANOVA es válida hacemos una comprobación post-hoc con el Tukey test:
TukeyHSD(aov(weight ~ group, data = PlantGrowth)) #Hay diferencias significativas comparando trt2 y trt2 (p=0.012)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = weight ~ group, data = PlantGrowth)
##
## $group
## diff lwr upr p adj
## trt1-ctrl -0.371 -1.0622161 0.3202161 0.3908711
## trt2-ctrl 0.494 -0.1972161 1.1852161 0.1979960
## trt2-trt1 0.865 0.1737839 1.5562161 0.0120064
#EJERCICIO_10
#Apartado_a
load("/Users/monicadoblasbajo/Library/Mobile Documents/com~apple~CloudDocs/3. Bioinformatics/UOC_MSc/2025_Software_análisis_datos/PEC_1/LAB_2/bodyfat.rda")
ls()
## [1] "airmiles" "airquality" "Bajo_Peso" "bodyfat" "días"
## [6] "Fumador" "iris" "medida" "mod_reg" "Orange"
## [11] "PlantGrowth" "tabla" "vect1" "vect2" "x1"
## [16] "y1"
head(bodyfat)
## density fat age weight height neck chest abdomen hip thigh knee ankle
## 1 1.0708 0.123 23 70.0295 172.085 36.2 93.1 85.2 94.5 59.0 37.3 21.9
## 2 1.0853 0.061 22 78.6555 183.515 38.5 93.6 83.0 98.7 58.7 37.3 23.4
## 3 1.0414 0.253 22 69.9160 168.275 34.0 95.8 87.9 99.2 59.6 38.9 24.0
## 4 1.0751 0.104 26 83.8765 183.515 37.4 101.8 86.4 101.2 60.1 37.3 22.8
## 5 1.0340 0.287 24 83.6495 180.975 34.4 97.3 100.0 101.9 63.2 42.2 24.0
## 6 1.0502 0.209 24 95.4535 189.865 39.0 104.5 94.4 107.8 66.0 42.0 25.6
## biceps forearm wrist
## 1 32.0 27.4 17.1
## 2 30.5 28.9 18.2
## 3 28.8 25.2 16.6
## 4 32.4 29.4 18.2
## 5 32.2 27.7 17.7
## 6 35.7 30.6 18.8
str(bodyfat)
## 'data.frame': 249 obs. of 15 variables:
## $ density: 'labelled' num 1.07 1.09 1.04 1.08 1.03 ...
## ..- attr(*, "label")= chr "Density"
## ..- attr(*, "units")= chr "g/cm^3"
## $ fat : 'labelled' num 0.123 0.061 0.253 0.104 0.287 0.209 0.192 0.124 0.041 0.117 ...
## ..- attr(*, "label")= chr "Body Fat"
## ..- attr(*, "units")= chr "Proportion"
## $ age : 'labelled' int 23 22 22 26 24 24 26 25 25 23 ...
## ..- attr(*, "label")= chr "Age"
## ..- attr(*, "units")= chr "years"
## $ weight : 'labelled' num 70 78.7 69.9 83.9 83.6 ...
## ..- attr(*, "label")= chr "Weight"
## ..- attr(*, "units")= chr "kg"
## $ height : 'labelled' num 172 184 168 184 181 ...
## ..- attr(*, "label")= chr "Height"
## ..- attr(*, "units")= chr "cm"
## $ neck : 'labelled' num 36.2 38.5 34 37.4 34.4 39 36.4 37.8 38.1 42.1 ...
## ..- attr(*, "label")= chr "Neck Circumference"
## ..- attr(*, "units")= chr "cm"
## $ chest : 'labelled' num 93.1 93.6 95.8 101.8 97.3 ...
## ..- attr(*, "label")= chr "Chest Circumference"
## ..- attr(*, "units")= chr "cm"
## $ abdomen: 'labelled' num 85.2 83 87.9 86.4 100 94.4 90.7 88.5 82.5 88.6 ...
## ..- attr(*, "label")= chr "Abdomen Circumference at Umbilicus"
## ..- attr(*, "units")= chr "cm"
## $ hip : 'labelled' num 94.5 98.7 99.2 101.2 101.9 ...
## ..- attr(*, "label")= chr "Hip Circumference"
## ..- attr(*, "units")= chr "cm"
## $ thigh : 'labelled' num 59 58.7 59.6 60.1 63.2 66 58.4 60 62.9 63.1 ...
## ..- attr(*, "label")= chr "Thigh Circumference"
## ..- attr(*, "units")= chr "cm"
## $ knee : 'labelled' num 37.3 37.3 38.9 37.3 42.2 42 38.3 39.4 38.3 41.7 ...
## ..- attr(*, "label")= chr "Knee Circumference"
## ..- attr(*, "units")= chr "cm"
## $ ankle : 'labelled' num 21.9 23.4 24 22.8 24 25.6 22.9 23.2 23.8 25 ...
## ..- attr(*, "label")= chr "Ankle Circumference"
## ..- attr(*, "units")= chr "cm"
## $ biceps : 'labelled' num 32 30.5 28.8 32.4 32.2 35.7 31.9 30.5 35.9 35.6 ...
## ..- attr(*, "label")= chr "Extended Biceps Circumference"
## ..- attr(*, "units")= chr "cm"
## $ forearm: 'labelled' num 27.4 28.9 25.2 29.4 27.7 30.6 27.8 29 31.1 30 ...
## ..- attr(*, "label")= chr "Forearm Circumference"
## ..- attr(*, "units")= chr "cm"
## $ wrist : 'labelled' num 17.1 18.2 16.6 18.2 17.7 18.8 17.7 18.8 18.2 19.2 ...
## ..- attr(*, "label")= chr "Wrist Circumference"
## ..- attr(*, "units")= chr "cm"
summary(bodyfat)
## density fat age weight
## Min. :0.995 Min. :0.0300 Min. :22.00 Min. : 56.75
## 1st Qu.:1.041 1st Qu.:0.1250 1st Qu.:36.00 1st Qu.: 72.30
## Median :1.055 Median :0.1920 Median :43.00 Median : 80.24
## Mean :1.055 Mean :0.1925 Mean :44.95 Mean : 81.39
## 3rd Qu.:1.070 3rd Qu.:0.2530 3rd Qu.:54.00 3rd Qu.: 89.44
## Max. :1.099 Max. :0.4750 Max. :81.00 Max. :164.87
## height neck chest abdomen
## Min. :162.6 Min. :31.10 Min. : 83.4 Min. : 70.40
## 1st Qu.:174.0 1st Qu.:36.40 1st Qu.: 94.6 1st Qu.: 85.20
## Median :178.4 Median :38.00 Median : 99.7 Median : 91.00
## Mean :178.7 Mean :38.03 Mean :100.9 Mean : 92.67
## 3rd Qu.:183.5 3rd Qu.:39.50 3rd Qu.:105.3 3rd Qu.: 99.20
## Max. :197.5 Max. :51.20 Max. :136.2 Max. :148.10
## hip thigh knee ankle
## Min. : 85.30 Min. :49.30 Min. :33.00 Min. :19.10
## 1st Qu.: 95.60 1st Qu.:56.10 1st Qu.:37.10 1st Qu.:22.00
## Median : 99.30 Median :59.00 Median :38.50 Median :22.80
## Mean : 99.94 Mean :59.45 Mean :38.61 Mean :23.12
## 3rd Qu.:103.50 3rd Qu.:62.30 3rd Qu.:39.90 3rd Qu.:24.00
## Max. :147.70 Max. :87.30 Max. :49.10 Max. :33.90
## biceps forearm wrist
## Min. :25.30 Min. :21.00 Min. :15.80
## 1st Qu.:30.30 1st Qu.:27.30 1st Qu.:17.60
## Median :32.10 Median :28.80 Median :18.30
## Mean :32.32 Mean :28.69 Mean :18.25
## 3rd Qu.:34.40 3rd Qu.:30.00 3rd Qu.:18.80
## Max. :45.00 Max. :34.90 Max. :21.40
#El dataset seleccionado contiene información de 15 diferentes variables relacionadas con medidas antropométricas para cuantificar la grsa corporal. El dataset contiene datos de personas desde los 22 a los 81 años, una media de p eso de 81,59kg y 1,78 m de altura.Aunque los datos parecen relativamente homogéneos, algunos valores como el mínimo y el máximo de abdomen, podrían corresonder a individuos con obesidad.
#Apartado_b
hist(bodyfat$fat,
main = "Distribución % de grasa corporal",
xlab = "Proporción de grasa corporal",
col = "lightblue")

boxplot(bodyfat$abdomen,
main = " Silueta del abdomen",
ylab = "cm", col = "lightgreen")

plot(density(bodyfat$weight),
main = "Peso corporal",
xlab = "Peso (kg)",
col = "purple", lwd = 2)

plot(density(bodyfat$height),
main = "Altura",
xlab = "Altura (cm)",
col = "lightpink", lwd = 2)

boxplot(bodyfat$age,
main = " Edad",
ylab = "años", col = "lightyellow")

#Apartado_c
ggplot(bodyfat, aes(x = fat)) +
geom_histogram(binwidth = 0.02, fill = "lightblue", color = "black") +
labs(title = "Distribución % de grasa corporal",
x = "% de grasa corporal",
y = "Frecuencia") +
theme_minimal()

#Aquí observarmos la distribuición de la variable 'fat' en la muestra donde la mayoría de individuos están entre el 10%-25% de grasa.
ggplot(bodyfat, aes(x = age)) +
geom_histogram(binwidth = 0.2, fill = "lightgreen", color = "black") +
labs(title = "Edad",
x = "Edad (años)",
y = "Frecuencia") +
theme_minimal()

#Aquí observarmos la distribuición de la variable 'age' en la muestra donde sigue una distribución normal, siendo los individuos entre 40-60 los más frecuentes en el dataset.
#Apartado_d
ggplot(bodyfat, aes(x = weight, y = fat)) +
geom_point(color = "black", size = 2) +
geom_smooth(method = "lm", se = TRUE, color = "darkorange") +
labs(title = "Relación entre peso y porcentaje de grasa corporal",
x = "Peso (kg)",
y = "% de grasa corporal") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

#Observamos una relación positiva entre los individuos de mayor peso y su % de grasa corporal. La línea azul representa la regresión y la sombra el intervalo de confianza.
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.