You are on page 1of 3

Clase 1.

Programación en R
Probabilidad y Estadı́stica Fundamental
Docente: Martı́n Macı́as
Agosto 2018

# ==== == = = = = = = = = = = = ==== CLASE PRACTICA 1 - LENGUAJE DE PROGRAMACION EN R ===== ==== ===== = = = = = = = #

# === Comandos iniciales


q () ; help ( q )

install . packages ( " ISwR " , dependencies = TRUE )


library ( ISwR )
getwd () ; setwd ( " / home / mamaciasq / Dropbox / Biostats " )

# === R como calculadora


# ( ver mas en www . statmethods . net / management / functions . html )
2+2 ; 3 * 4 ; 5 / 10 ; (10 * 4) / 2 ; (2^2 * 3) / ( exp (0)) ; 2 / 2+2 -2 * 2 ; log10 (10^2)
log ( exp (2)) ; sqrt (2) ; 2^(1 / 2) ; sin ( pi ) ; cos ( pi ) ; round ( pi ,4) ; abs ( -5)

# === Almacenamiento de valores intermedios


X^x
x <- 2; X = 4
X^x

ls ()
class ( x )

# === Concatenacion de valores y relaciones

sex <- c ( " M " ," M " ," M " ," M " ," H " ," H " )
esc <- c ( rep ( " Primaria " ,3) , " Secundaria " , rep ( " Universitario " ,2))

sex <- factor ( sex )


esc <- ordered ( esc )

weight <- c (60 , 72 , 57 , 90 , 95 , 72)


weight
class ( weight )

height <- c (1.75 , 1.80 , 1.65 , 1.90 , 1.74 , 1.91) ; height ; class ( height )
bmi <- weight / height ^2; bmi ; class ( bmi )

height > 1.90


height >= 1.90
sum ( height >= 1.90)
sum ( height == 1.90)
sum ( height ! = 1.90)
sum ( height >=1.80 & height <=1.90)

datos = data . frame ( Peso = weight , Estatura = height ,


IMC = bmi , Sexo = sex ,
Escolaridad = esc )

1
# === Distribuciones de frecuencias ( tabular y grafica )

table ( sex )
prop . table ( table ( esc ))
pie ( table ( esc ))

barplot ( table ( sex ))


barplot ( prop . table ( table ( esc )) , col = rainbow (3))

stem ( weight )
stem ( height )

hist ( height )
boxplot ( height )

boxplot ( Estatura ~ Sexo , data = datos )


boxplot ( Estatura ~ Escolaridad , data = datos )

# === Descripcion de datos con medidas numericas

# === Tendencia central


sum ( weight )
sum ( weight ) / length ( weight )

n <- length ( weight )


total <- sum ( weight )
xbar <- total / n

weight . ordenado <- sort ( weight )


xmed <- ( weight . ordenado [3] + weight . ordenado [4]) / 2

x . mod <- names ( which . max ( table ( weight )))

mean ( weight )
median ( weight )
quantile ( weight , probs = c (0.25 ,0.5 ,0.75))

# === Dispersion

rango <- max ( weight ) - min ( weight )

desvios . media <- weight - xbar ; desvios . media


desvios .2 <- desvios . media ^2
SS <- sum ( desvios .2)

varianza <- SS / ( length ( weight ) - 1)


desv . est <- sqrt ( varianza )

coef . var <- desv . est / xbar

IQR . weight <- quantile ( weight , probs = c (0.75)) - quantile ( weight , probs = c (0.25))

range ( weight )
var ( weight )
sd ( weight )
cv <- sd ( weight ) / xbar
IQR ( weight )

# === Forma
install . packages ( " e1071 " , dependencies = T ) ; library ( e1071 )
skewness ( weight , type =1)
kurtosis ( weight , type =1)

2
# === Datos de ejemplo

sex <- c ( " M " ," M " ," M " ," H " ," H " ," M " ," M " ," M " ," H " ," H " )
esc <- c ( rep ( " Primaria " ,4) , " Secundaria " , rep ( " Universitario " ,2) , rep ( " Secundaria " ,3))

sex <- factor ( sex )


esc <- ordered ( esc )

weight <- c (60 , 72 , 95 , 72 , 88 , 64 , 89 , 92 , 50 , 66 )


height <- c (1.75 ,1.80 ,1.65 ,1.90 ,1.74 ,1.91 ,1.50 ,1.66 ,1.92 ,1.55)

bmi <- weight / height ^2

datos = data . frame ( Peso = weight , Estatura = height , IMC = bmi , Sexo = sex , Escolaridad = esc )

# === Distribuciones de frecuencias bivariadas ( tabular y grafica )

# Cualitativa - cualitativa

table ( sex , esc )


barplot ( table ( sex , esc ) , col = terrain . colors (2) ,
main = " Escolaridad vs Sexo al nacer " , legend . text = TRUE ,
xlab = " Escolaridad " , ylab = " Frecuencia Absoluta " )

barplot ( table ( sex , esc ) , col = terrain . colors (2) , beside = TRUE ,
main = " Escolaridad vs Sexo al nacer " , legend . text = TRUE ,
xlab = " Escolaridad " , ylab = " Frecuencia Absoluta " )

# Cualitativa - cuantitativa

boxplot ( IMC ~ Sexo , data = datos , main = " IMC vs Sexo al nacer " ,
ylab = " IMC " , xlab = " Sexo " )

boxplot ( IMC ~ Escolaridad , data = datos , main = " Escolaridad vs Sexo al nacer " ,
ylab = " IMC " , xlab = " Escolaridad " )

# Cuantitativa - cuantitativa

plot ( Peso ~ Estatura , data = datos , main = " Peso vs Estatura " )

datos . cuanti <- datos [ , c ( " Peso " ," Estatura " ," IMC " )]
plot ( datos . cuanti , main = " Matriz de dispersion " )

# === Medidas descriptivas bivariadas

# Cualitativa - cualitativa

prop . table ( table ( sex , esc ))


prop . table ( table ( sex , esc ) ,1)
prop . table ( table ( sex , esc ) ,2)

# Cualitativa - cuantitativa

tapply ( bmi , sex , summary )


tapply ( bmi , esc , summary )

# Cuantitativa - cuantitativa
cov ( datos . cuanti )
cor ( datos . cuanti )

You might also like