Professional Documents
Culture Documents
• Probabilidad
library(readxl)
Titanic<-read_excel("Pasajeros-Titanic.xlsx")
Titanic
dim(Titanic)
## [1] 1309 5
names(Titanic)
class(Titanic)
## [1] "data.frame"
class(Titanic$sexo)
## [1] "character"
options(knitr.table.format="latex")
library(pander)
t1<-table(Titanic$sobrevivio,Titanic$clase)
t1
##
## 1st 2nd 3rd
## no 123 158 528
## yes 200 119 181
t2<-addmargins(t1)
t2
##
## 1st 2nd 3rd Sum
## no 123 158 528 809
## yes 200 119 181 500
## Sum 323 277 709 1309
pander(t2)
library(lattice)
library(mosaic)
##
## Attaching package: 'dplyr'
##
## Attaching package: 'ggstance'
##
## New to ggformula? Try the tutorials:
## learnr::run_tutorial("introduction", package = "ggformula")
## learnr::run_tutorial("refining", package = "ggformula")
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected
by this.
##
## Note: If you use the Matrix package, be sure to load it BEFORE loading mosaic.
##
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
##
## mean
# Diagrama de barras
bargraph(~clase, data = Titanic, main= "Gráfico de Barras")
X=c(2,3,4,4.5,4.5,5.6,5.7,5.8,6,6.1,6.5,7,7,7,7.5,7.5,7.5,8.3,9,10.2,10.4,11,11.1,11.5,1
2,13)
div<-table(cut(X,breaks = 7))
div
##
## (1.99,3.57] (3.57,5.14] (5.14,6.71] (6.71,8.29] (8.29,9.86] (9.86,11.4]
## 2 3 6 6 2 4
## (11.4,13]
## 3
class(div)
## [1] "table"
library(pander)
library(xtable)
mat<-data.frame(div)
names(mat)<-c("Intervalos","Frecuencias($n_i$)")
x<-xtable(mat)
pander(x)
Intervalos Frecuencias(ni )
(1.99,3.57] 2
(3.57,5.14] 3
(5.14,6.71] 6
(6.71,8.29] 6
(8.29,9.86] 2
(9.86,11.4] 4
(11.4,13] 3
# División en intervalos
##
## (0.0869,13.5] (13.5,26.8] (26.8,40.1] (40.1,53.4] (53.4,66.7]
## 99 375 345 150 68
## (66.7,80.1]
## 9
library(mosaic)
histogram(~edad, data = Titanic)
x=Titanic$edad
op<-par(mfrow=c(2,2)) #pone gráficos en formato 2 x 2
hist(x,breaks="Sturges", col = "purple") # número de intervalos según la fórmula de Stur
ges
hist(x, breaks=4, col = "green")
hist(x, breaks = 10, col = "yellow")
hist(x,breaks = 35,col = "grey")
library(mosaic)
library(gridExtra)
##
## Attaching package: 'gridExtra'
p1<-freqpolygon(~edad, Titanic)
p2<-histogram(~edad, Titanic)
p3<-ladd(panel.freqpolygon(Titanic$edad))
grid.arrange(p1,p2,p3,ncol=2)
par(op)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
dt<-data.frame(Titanic)
ggplot(dt,aes(x=clase))+geom_bar( aes(fill=sobrevivio), position="dodge")
library(HSAUR)
mort<-data.frame(water)
ggplot()+geom_point(data=mort, aes(x=hardness, y=mortality, colour=location))
1-10 of 10 rows
cov(mort$hardness,mort$mortality)
## [1] -4681.544
cor(mort$hardness,mort$mortality)
## [1] -0.6548486
## [1] 0.4288267
X<-seq(-1,1,length=100)
Y=sqrt(1-X^2)
xy=data.frame(X,Y)
ggplot(data=xy,aes(x=X, y=Y)) + geom_point(color="darkorange", size=3)
cor(X,Y)
## [1] -3.162392e-16
library(plotly)
library(readxl)
library(plotly)
library(ggplot2)
record_100_m <- read_excel("record-100-m.xlsx")
dt=data.frame(x=record_100_m[,1], y=record_100_m[,2])
names(dt)<-c("anyo","record")
colour
size
10.50
10.25
record
10.00
9.75
Probabilidad
library(pander)
x=sample(1:2,1000, replace = TRUE) # Error in sample.int(length(x), size, replace, prob)
: invalid 'replace' argument
x
## [1] 2 2 1 1 1 2 2 1 2 1 1 1 2 1 1 1 1 1 2 1 2 1 2 1 2 1 2 2 2 2 2 1 1 1
## [35] 1 1 1 2 1 1 2 2 2 2 1 2 1 1 2 1 2 2 2 1 2 2 1 1 2 1 1 2 2 2 2 1 1 1
## [69] 1 1 1 2 2 1 2 1 2 2 2 2 1 2 1 1 1 1 2 2 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 1 2 2 6 5 2 6 1 3
## [987] 6 1 4 6 2 4 5 5 4 2 2 2 5 2
pander(table(y))
1 2 3 4 5 6
library(dygraphs)
mdeaths
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1974 2134 1863 1877 1877 1492 1249 1280 1131 1209 1492 1621 1846
## 1975 2103 2137 2153 1833 1403 1288 1186 1133 1053 1347 1545 2066
## 1976 2020 2750 2283 1479 1189 1160 1113 970 999 1208 1467 2059
## 1977 2240 1634 1722 1801 1246 1162 1087 1013 959 1179 1229 1655
## 1978 2019 2284 1942 1423 1340 1187 1098 1004 970 1140 1110 1812
## 1979 2263 1820 1846 1531 1215 1075 1056 975 940 1081 1294 1341
fdeaths
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1974 901 689 827 677 522 406 441 393 387 582 578 666
## 1975 830 752 785 664 467 438 421 412 343 440 531 771
## 1976 767 1141 896 532 447 420 376 330 357 445 546 764
## 1977 862 660 663 643 502 392 411 348 387 385 411 638
## 1978 796 853 737 546 530 446 431 362 387 430 425 679
## 1979 821 785 727 612 478 429 405 379 393 411 487 574
lungDeaths<-cbind(mdeaths,fdeaths)
dygraph(lungDeaths)
mdeaths fdeaths
2800
2600
2400
2200
2000
1800
1600
1400
1200
1000
800
600
400
200
Jan 1974 Jan 1975 Jan 1976 Jan 1977 Jan 1978 Jan 1979
library(pander)
x=sample(1:2,1000, replace=TRUE)
pander(table(x))
1 2
495 505
barplot(table(x))
1 2 3 4 5 6
1 2 3 4 5 6
x=sample(1:6,1000, replace=TRUE)
y=table(x)
pander(y)
1 2 3 4 5 6
1-pbinom(3,10,0.5)
## [1] 0.828125