You are on page 1of 13

ejercicio_1.

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)

## Loading required package: timeDate


## Loading required package: timeSeries
##
## Rmetrics Package fBasics
## Analysing Markets and calculating Basic Statistics
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
library(e1071)

##
## 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)

## Loading required package: leaps


## Loading required package: locfit
## locfit 1.5-9.1 2013-03-22
## Loading required package: mgcv
## Loading required package: nlme
## This is mgcv 1.8-17. For overview type 'help("mgcv-package")'.
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:e1071':
##
## kurtosis, skewness
## The following objects are masked from 'package:timeDate':
##
## kurtosis, skewness
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
library(itsmr)

##
## 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)

## Loading required package: lattice


library(PASWR)

## Loading required package: MASS


##
## Attaching package: 'MASS'
## The following object is masked from 'package: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)

#1 cargamos un archivo elavorado en excel con los 50 datos


#con la funcion "read_excel" y a suvez lo almacenamos en una variable
rat <- read_excel("C:/Users/Personal/Desktop/ejer_Ratas/datos.xlsx")
rat

## # 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] 0.03 3.53


r<-(3.53-0.03)
r

## [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))

## hora Freq FreqAc Rel RelAc


## 1 0.03 2 2 0.04 0.04
## 2 0.04 1 3 0.02 0.06
## 3 0.05 1 4 0.02 0.08
## 4 0.07 1 5 0.02 0.10
## 5 0.11 1 6 0.02 0.12
## 6 0.12 1 7 0.02 0.14
## 7 0.14 1 8 0.02 0.16
## 8 0.22 2 10 0.04 0.20
## 9 0.23 1 11 0.02 0.22
## 10 0.24 1 12 0.02 0.24
## 11 0.29 2 14 0.04 0.28
## 12 0.31 1 15 0.02 0.30
## 13 0.33 1 16 0.02 0.32
## 14 0.36 1 17 0.02 0.34
## 15 0.47 1 18 0.02 0.36
## 16 0.51 1 19 0.02 0.38
## 17 0.6 1 20 0.02 0.40
## 18 0.61 1 21 0.02 0.42
## 19 0.73 1 22 0.02 0.44
## 20 0.85 1 23 0.02 0.46
## 21 0.86 2 25 0.04 0.50
## 22 0.93 1 26 0.02 0.52
## 23 0.97 1 27 0.02 0.54
## 24 0.99 1 28 0.02 0.56
## 25 1.05 1 29 0.02 0.58
## 26 1.06 1 30 0.02 0.60
## 27 1.11 1 31 0.02 0.62
## 28 1.14 1 32 0.02 0.64
## 29 1.18 1 33 0.02 0.66
## 30 1.21 1 34 0.02 0.68
## 31 1.35 1 35 0.02 0.70
## 32 1.4 1 36 0.02 0.72
## 33 1.44 1 37 0.02 0.74
## 34 1.71 1 38 0.02 0.76
## 35 1.79 1 39 0.02 0.78
## 36 1.88 1 40 0.02 0.80
## 37 1.91 1 41 0.02 0.82
## 38 1.93 1 42 0.02 0.84
## 39 1.96 1 43 0.02 0.86
## 40 2.21 1 44 0.02 0.88
## 41 2.34 1 45 0.02 0.90
## 42 2.63 1 46 0.02 0.92
## 43 2.66 1 47 0.02 0.94
## 44 2.93 1 48 0.02 0.96
## 45 3.2 1 49 0.02 0.98

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))

## x Freq FreqAc Rel Rela RelAcs RelAc


## 1 (0.0265,0.53] 19 19 0.38 38 38 0.38
## 2 (0.53,1.03] 9 28 0.18 18 56 0.56
## 3 (1.03,1.53] 9 37 0.18 18 74 0.74
## 4 (1.53,2.03] 6 43 0.12 12 86 0.86
## 5 (2.03,2.53] 2 45 0.04 4 90 0.90
## 6 (2.53,3.03] 3 48 0.06 6 96 0.96
## 7 (3.03,3.53] 2 50 0.04 4 100 1.00
#creamos una funcion
tdfA <- function(y) {# Los datos deben de estar guardados en un vector
# Calculamos el nmero de clases usando la frmula de Sturge
k <- nclass.Sturges(y)
# Formamos los intervalos de clase
intervalos <- cut(y, breaks = k)
# Tabla de frecuencias absolutas
tabla2 <- as.data.frame(table(intervalos))
# Aadimos frecuencias acumuladas y frecuencias relativas
tabla2 <- transform(tabla2, F.Acum = cumsum(Freq), F.R = prop.table(Freq))
# Aadimos frecuencias relativas acumuladas
tabla2 <- transform(tabla2, F.R.Acum = cumsum(F.R))
# Multiplicamos las frecuencas relativas por 100
tabla2 <- transform(tabla2, FR.Porc = 100*F.R, FRA.Porc = 100*F.R.Acum)
# Para obtener la marca de clase de los intervalos de clase, usaremos
midpoints <- function(x, dp=2){
lower <- as.numeric(gsub(',.*','',gsub('\\(|\\[|\\)|\\]','', x)))
upper <- as.numeric(gsub('.*,','',gsub('\\(|\\[|\\)|\\]','', x)))
return(round(lower+(upper-lower)/2, dp))
}
# Agregamos la columna de puntos medios de los intervalos de clase
tabla2 <- transform(tabla2, MC = midpoints(intervalos))
tabla2
}

6
tdfA(rat$hora)

## intervalos Freq F.Acum F.R F.R.Acum FR.Porc FRA.Porc MC


## 1 (0.0265,0.53] 19 19 0.38 0.38 38 38 0.28
## 2 (0.53,1.03] 9 28 0.18 0.56 18 56 0.78
## 3 (1.03,1.53] 9 37 0.18 0.74 18 74 1.28
## 4 (1.53,2.03] 6 43 0.12 0.86 12 86 1.78
## 5 (2.03,2.53] 2 45 0.04 0.90 4 90 2.28
## 6 (2.53,3.03] 3 48 0.06 0.96 6 96 2.78
## 7 (3.03,3.53] 2 50 0.04 1.00 4 100 3.28
#PARA CALCULAR LA MEDIA DE NSTROS DATOS UTILIZAMOS mean().
mean(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.03 0.22 0.29 0.86


#PARA CALCLAR LA MEDIANA DE NUESTROS DATOS UTILIZAMOS median().
median(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)

## 0% 25% 50% 75% 100%


## 0.0300 0.2900 0.8950 1.6425 3.5300
#PARA CALCULAR EL COEFICIENTE DE CURTOSIS UTILIZAREMOS kurtosis().
kurtosis(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")

# Dibuja las l?neas que unen los puntos


lines(puntosX,
puntosY)
abline(v=2.92,col="green")
abline(v=4.53,col="green")

9
1.0
Frecuencia relativa acumulada

0.8
0.6
0.4
0.2
0.0

(0.0265,0.53] (1.03,1.53] (2.03,2.53] (3.03,3.53]

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")

# Dibuja las l?neas que unen los puntos


lines(puntosX,
puntosY)
abline(v=0.99,col="green")
abline(v=5.06,col="green")

10
1.0
Frecuencia relativa acumulada

0.8
0.6
0.4
0.2
0.0

(0.0265,0.53] (1.03,1.53] (2.03,2.53] (3.03,3.53]

HORAS

boxplot(rat$hora,main="Diagrama de caja",horizontal = T)

11
Diagrama de caja

0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5

#DIAGRAMA TALLO HOJA.


stem(rat$hora)

##
## 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

You might also like