Professional Documents
Culture Documents
R
Personal
Tue May 30 10:11:20 2017
#Los siguientes datos corresponden alos tiempos de vida (en horas) de unas ratitas
#de laboratorio expuestas a cierto veneno, ademas se facilita la tabal de distribucion
#de frecuencias. Se queire ver la efectividad de dicho veneno.
setwd("~/")
library(fBasics)
##
## Attaching package: 'e1071'
## The following objects are masked from 'package:timeDate':
##
## kurtosis, skewness
library(knitr)
library(xtable)
##
## Attaching package: 'xtable'
## The following object is masked from 'package:timeSeries':
##
## align
## The following object is masked from 'package:timeDate':
##
## align
library(fmsb)
library(plot3D)
library(tseries)
library(astsa)
##
## Attaching package: 'astsa'
1
## The following object is masked from 'package:fBasics':
##
## nyse
library(TSA)
##
## Attaching package: 'itsmr'
## The following objects are masked from 'package:TSA':
##
## periodogram, season
## The following object is masked from 'package:tseries':
##
## arma
library(IPSUR)
library(DAAG)
2
## hills
## The following object is masked from 'package:itsmr':
##
## deaths
##
## Attaching package: 'PASWR'
## The following object is masked from 'package:DAAG':
##
## biomass
library(foreign)
library(readxl)
## # A tibble: 50 x 1
## hora
## <dbl>
## 1 0.03
## 2 0.03
## 3 0.04
## 4 0.05
## 5 0.07
## 6 0.11
## 7 0.12
## 8 0.14
## 9 0.22
## 10 0.22
## # ... with 40 more rows
#muestra los valores para la variable horas
rat$hora
## [1] 0.03 0.03 0.04 0.05 0.07 0.11 0.12 0.14 0.22 0.22 0.23 0.24 0.29 0.29
## [15] 0.31 0.33 0.36 0.47 0.51 0.60 0.61 0.73 0.85 0.86 0.86 0.93 0.97 0.99
## [29] 1.05 1.06 1.11 1.14 1.18 1.21 1.35 1.40 1.44 1.71 1.79 1.88 1.91 1.93
## [43] 1.96 2.21 2.34 2.63 2.66 2.93 3.20 3.53
#con nclass.sturges calculamos en numero de clases, lo almacenamos en k
k<-nclass.Sturges(rat$hora)
k
## [1] 7
#calculamos en rango limite superior menos inferior
range(rat)
## [1] 3.5
3
#calculamos en intevarlo de clase en este caso para 7 clases como nos pide el ejercicio
i<-(r/7)
i
## [1] 0.5
# Agrupamos los valores de la variable hora en tablaCyl y calcula su frecuencia absoluta
tablaCyl <- as.data.frame(table(hora = rat$hora))
tablaCyl
## hora Freq
## 1 0.03 2
## 2 0.04 1
## 3 0.05 1
## 4 0.07 1
## 5 0.11 1
## 6 0.12 1
## 7 0.14 1
## 8 0.22 2
## 9 0.23 1
## 10 0.24 1
## 11 0.29 2
## 12 0.31 1
## 13 0.33 1
## 14 0.36 1
## 15 0.47 1
## 16 0.51 1
## 17 0.6 1
## 18 0.61 1
## 19 0.73 1
## 20 0.85 1
## 21 0.86 2
## 22 0.93 1
## 23 0.97 1
## 24 0.99 1
## 25 1.05 1
## 26 1.06 1
## 27 1.11 1
## 28 1.14 1
## 29 1.18 1
## 30 1.21 1
## 31 1.35 1
## 32 1.4 1
## 33 1.44 1
## 34 1.71 1
## 35 1.79 1
## 36 1.88 1
## 37 1.91 1
## 38 1.93 1
## 39 1.96 1
## 40 2.21 1
## 41 2.34 1
## 42 2.63 1
## 43 2.66 1
## 44 2.93 1
4
## 45 3.2 1
## 46 3.53 1
# Muestra la tabla de frecuencias completa redondeando las frecuencias relativas a 3 decimales
transform(tablaCyl,
FreqAc = cumsum(Freq),
Rel = round(prop.table(Freq), 3),
RelAc = round(cumsum(prop.table(Freq)), 3))
5
## 46 3.53 1 50 0.02 1.00
# Agrupa los valores de la variable hora (mpg) en 7 clases y calcula su frecuencia absoluta
tablaMpg <- as.data.frame(table(x = factor(cut(rat$hora, breaks=7))))
tablaMpg
## x Freq
## 1 (0.0265,0.53] 19
## 2 (0.53,1.03] 9
## 3 (1.03,1.53] 9
## 4 (1.53,2.03] 6
## 5 (2.03,2.53] 2
## 6 (2.53,3.03] 3
## 7 (3.03,3.53] 2
# Muestra la tabla de frecuencias completa redondeando las frecuencias relativas a 3 decimales
transform(tablaMpg,
FreqAc = cumsum(Freq),
Rel = round(prop.table(Freq), 3),
Rela= round(prop.table(Freq), 3)*100,
RelAcs = round(cumsum(prop.table(Freq)), 3)*100,
RelAc = round(cumsum(prop.table(Freq)), 3))
6
tdfA(rat$hora)
## [1] 1.0616
#PARA CALCULAR LA MODA PRIMERO LLAMAREMOS A UNA LIBRERIA "modeest".
library(modeest)
##
## This is package 'modeest' written by P. PONCET.
## For a complete list of functions, use 'library(help = "modeest")' or 'help.start()'.
##
## Attaching package: 'modeest'
## The following object is masked from 'package:TSA':
##
## skewness
## The following object is masked from 'package:e1071':
##
## skewness
## The following objects are masked from 'package:fBasics':
##
## ghMode, hypMode, nigMode
## The following object is masked from 'package:timeDate':
##
## skewness
#UNA VEZ ECHO ESTO CALCULAREMOS LA MODA UTILIZADO mfv().
mfv(rat$hora)
## [1] 0.895
#PARA CALCULAR NUESTRA TABLA DE FRECUAENCIAS ABSOLUTAS UTILIZAREMOS table().
table(rat$hora)
##
## 0.03 0.04 0.05 0.07 0.11 0.12 0.14 0.22 0.23 0.24 0.29 0.31 0.33 0.36 0.47
## 2 1 1 1 1 1 1 2 1 1 2 1 1 1 1
## 0.51 0.6 0.61 0.73 0.85 0.86 0.93 0.97 0.99 1.05 1.06 1.11 1.14 1.18 1.21
## 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1
7
## 1.35 1.4 1.44 1.71 1.79 1.88 1.91 1.93 1.96 2.21 2.34 2.63 2.66 2.93 3.2
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 3.53
## 1
#UNA VEZ ECHO ESTO CALCULAREMOS LOS PERCENTILES UTILIZANDO percentile().
percentile(rat$hora)
## [1] 2 2 5 7 9 11 13 15 18 18 21 23 26 26 29 31 33
## [18] 35 37 39 41 43 45 48 48 52 54 56 58 60 62 64 66 68
## [35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
#PARA CALCULAR LOS CUARTILES UTILIZAREMOS quantile().
quantile(rat$hora)
## [1] -0.003739954
#PARA CALCULAR EL COEFICIENTE DE ASIMETRIA UTILIZAREMOS skewness().
skewness(rat$hora)
## [1] 0.8689941
## attr(,"method")
## [1] "moment"
#PARA CALCULAR LA VARIANZA UTILIZAREMOS var().
var(rat$hora)
## [1] 0.8395198
#PARA CALCULAR LA DESVIACION ESTANDAR UTILIZAREMOS sd().
sd(rat$hora)
## [1] 0.9162532
#PARA CALCULAR EL COEFICIENTE DE VARIACIN UTILIZAREMOS LA SIGUIENTE OPERCIN.
CV=sd(rat$hora)/mean(rat$hora)
CV
## [1] 0.863087
#PARA CALCULAR EL ERRO TIPICO UTILIZAREMOS LA SIGUIENTE OPERCIN.
ET=sd(rat$hora)/(length(rat$hora)-1)^(1/2)
ET
## [1] 0.1308933
#los percentiles 30 y 70
quantile(rat$hora,.30)
## 30%
## 0.324
p<-quantile(rat$hora,.30)
p
## 30%
8
## 0.324
quantile(rat$hora,.70)
## 70%
## 1.365
l<-quantile(rat$hora,.70)
l
## 70%
## 1.365
# Agrupa los valores de la variable hora) en 5 clases y calcula su frecuencia absoluta
tablaMpg <- table(hora = factor(cut(rat$hora, breaks=7)))
# Crea dos vectores con las coordenadas x e y de los puntos que se quieren mostrar
puntosX <- as.data.frame(tablaMpg)$hora
puntosY <- cumsum(prop.table(tablaMpg[]))
# Dibuja los puntos
plot(puntosX,
puntosY,
ylim=c(0,1),
xlab="HORAS",
ylab="Frecuencia relativa acumulada")
9
1.0
Frecuencia relativa acumulada
0.8
0.6
0.4
0.2
0.0
HORAS
#los percentiles 30 y 70
quantile(rat$hora,.0)
## 0%
## 0.03
quantile(rat$hora,.90)
## 90%
## 2.369
# Crea dos vectores con las coordenadas x e y de los puntos que se quieren mostrar
puntosX <- as.data.frame(tablaMpg)$hora
puntosY <- cumsum(prop.table(tablaMpg[]))
# Dibuja los puntos
plot(puntosX,
puntosY,
ylim=c(0,1),
xlab="HORAS",
ylab="Frecuencia relativa acumulada")
10
1.0
Frecuencia relativa acumulada
0.8
0.6
0.4
0.2
0.0
HORAS
boxplot(rat$hora,main="Diagrama de caja",horizontal = T)
11
Diagrama de caja
##
## The decimal point is at the |
##
## 0 | 00011111222233334
## 0 | 556679999
## 1 | 00111122444
## 1 | 78999
## 2 | 023
## 2 | 679
## 3 | 2
## 3 | 5
#PARA EL HISTOGRAMA UTILIZAREMOS hist().
tablaMpg
## hora
## (0.0265,0.53] (0.53,1.03] (1.03,1.53] (1.53,2.03] (2.03,2.53]
## 19 9 9 6 2
## (2.53,3.03] (3.03,3.53]
## 3 2
hist(rat$hora,col="blue",main="HISTOGRAMA FRECUENCIAS",xlab="horas",ylab="frecuencia absoluta")
12
HISTOGRAMA FRECUENCIAS
15
frecuencia absoluta
10
5
0
0 1 2 3 4
horas
13