** Funciones en R
En R, cada distribucion de probabilidad se nombra mediante una palabra clave o alias. Las palabras clave para las distribuciones mas importantes son:
\[ \begin{array}{l|l|l|c} \text{Funcion} & \text{Significado} & \text{uso} & \text{Observacion}\\ \hline p & \text{probability} & \text{calcula probabilidades acumuladas (cdf)} & \text{---}\\ q & \text{quantile} & \text{calcula cuantiles (percentiles)} & \text{---}\\ d & \text{density} & \text{calcula probabilidades puntaules} & \text{solo uso grafico en el caso continuo}\\ r & \text{random} & \text{Genera datos aleatorios segun una distribucion especifica} & \text{---}\\ \hline \end{array} \] Distribucion Exponencial
#representa la densidad de una exponencial de media 1 entre 0 y 10
curve(dexp(x), from=0, to=10)
Distribucion binomial
x = rbinom(20, 1, 0.5)
x
## [1] 1 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 1
#Genera 20 observaciones con distribucion B(1, 0.5)
Contando exitos vs fracasos
table(x)
## x
## 0 1
## 8 12
ejemplo de Distribucion normal
si \(x\) es una variable aleatoria, con distribucion normal de media 3, y su desviacion tipica es de 0.5, la proabilidad de que \(x\) sea menor que 3.5 se calcula en R de esta forma.
pnorm(3.5, mean=3, sd=0.5)
## [1] 0.8413447
qnorm(0.7)
## [1] 0.5244005
qnorm(0.7, sd=0.5)
## [1] 0.2622003
El valor \(z_\alpha\) que aparece en muchas de las formulas para intervalos y contrastes se obtiene con el comando qnorm(1-alfa). Algunos ejemplos:
qnorm(0.975)
## [1] 1.959964
x = rnorm(100, mean = 10,sd=1)
x
## [1] 8.696037 9.538392 10.543431 7.645993 7.905812 10.055101 11.046090
## [8] 10.357462 12.539633 10.157261 11.665071 11.109791 10.705463 6.802049
## [15] 9.867955 11.202642 9.729936 9.994896 7.922226 9.894164 10.379549
## [22] 8.017786 10.551569 11.302078 9.347329 9.923483 10.966867 9.542270
## [29] 9.837748 10.169591 10.273554 10.672775 10.440924 10.856056 11.622389
## [36] 11.167284 9.957751 9.544282 9.849488 10.825815 9.629017 10.580094
## [43] 13.086748 8.633195 10.323190 9.937607 9.953827 10.026022 9.681955
## [50] 12.109034 10.227727 10.191375 11.323352 8.476410 9.718514 10.006387
## [57] 13.121302 9.075335 10.416791 10.516406 10.920136 11.538969 9.742902
## [64] 9.513030 11.360020 9.833001 10.817338 9.185170 10.857298 9.167178
## [71] 10.931103 10.485541 7.953826 10.375621 8.263906 9.411433 10.940910
## [78] 9.681405 9.261586 12.713837 10.085638 9.821605 8.212703 10.220738
## [85] 10.357685 10.563465 8.967679 11.067289 10.321149 10.903263 10.909191
## [92] 9.506733 11.057253 8.679128 10.872613 9.923911 11.075085 10.574608
## [99] 9.511605 9.756501
mean(x)
## [1] 10.15004
hist(x)
boxplot(x)
hist(x, freq=FALSE) #Freq=FALSE, para que el area del histograma sea 1
curve(dnorm(x, mean = 10,sd=1),from=7, to=13, add=TRUE)
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:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.