Sunteți pe pagina 1din 5

#C1***ANOVA A DOS VIAS***

rm(list=ls()) #Borrar memoria


setwd("D:/manuel.contento/Desktop/AMV_2017II/AMV_sem9")
DataGSI <- read.csv("GSI.csv", header = T, sep=";",dec=".")
#Convertir Fotoperiodo y Temperatura en Factores
Fotoperiodo <- factor(Fotoperiodo)
levels(Fotoperiodo) <- c("9horas", "14horas")
Fotoperiodo
Temperatura <- factor(Temperatura)
levels(Temperatura) <- c("16°C", "27°C")
Temperatura
#Tabla anova
g <- lm(GSI ~ Fotoperiodo + Temperatura + Fotoperiodo:Temperatura)
anova(g)
#Prueba de Shapiro para Residuales
shapiro.test(residuals(g))
#Homocedasticidad en Fotoperiodo y Temperatura
require(car)
leveneTest(GSI~Fotoperiodo)
leveneTest(GSI~Temperatura)
#Comparaciones multiples
model_AB<-aov(GSI~Fotoperiodo+Temperatura)
tapply(GSI,Fotoperiodo,mean) #Promedios por Fotoperiodo
TukeyHSD(model_AB,"Fotoperiodo")
tapply(GSI,Temperatura,mean) #Promedios por Temperatura
TukeyHSD(model_AB,"Temperatura")
#C2***COV COR****
databody <- read.table (“atropometria.csv, header = TRUE)
sumary (databody)
var (databody)
x <- databody [, c(“chest”, “waist”, “hips”)]; x

#Matriz covarianza
var (x)
cor (x)

#Matriz de covarianza por género


cov (subset (databody, gender == “male”) [, c(“chest”, “waist”, “hips”)])
cov (subset (databody, gender == “female”) [, c(“chest”, “waist”, “hips”)])

#Matriz de correlación
mc <- cor(databody[, c(“chest”, “waist”, “hips”)]); mc
round(mc,4) #Redondeando a cuatro decimales

#Matriz correlacion por genreo


cor (subset (databody, gender == “male”) [, c(“chest”, “waist”, “hips”)])
cor (subset (databody, gender == “female”) [, c(“chest”, “waist”, “hips”)])

#Distancias
x <- databody [, c(“chest”, “waist”, “hips”)]; x
escalado <- scale (x, center = F, scale = T; escalado
escalado1 <- scale (x, center = F); escalado1
sqrt (sum(databody$chest^2)/(length(databody$chest)-1))
sqrt (sum(databody$waist^2)/(length(databody$waist)-1))
sqrt (sum(databody$hips^2)/(length(databody$hipst)-1))

#Distancias sobre los valores no centrados y escalados


round (dist (scale (x, center =F)), 2)
round (dist (scale (x, center =F, scale = T)), 2)

#Distancias sobre valores estandarizados (x-m)/a


round (dist (scale (x, center = T, scale = T)), 2)

#Distancias sobre calores no centrados pero escalados por s (x/s)


round (dist (scale (x, center =F, scale = apply (x, 2, sd, na.rm = TRUE))), 2)
#C2***MATRIZ DE CORRELACION***
mc <- cor(databody[, c(“chest”, “waist”, “hips”)]); mc
round(mc,4)#Redondeando a cuatro decimales
#matriz correlacion por genreo
cor(subset(databody, gender == “male”) [, c(“chest”, “waist”, “hips”)])
#Escalado en r
escalado<- scale(databody [, c(“chest”, “waist”, “hips”)], center = F, scale=T; escalado
x <-datbody[, c(“chest”, “waist”, “hips”)]; x
round (dist(scale(databody [, c(“chest”, “waist”, “hips”)], center = F) ), 2)
#***VISUALIZACIÓN***
#Diagrama de dispersión Scatterplot
mlab <- "Empresas Manufactureras con 20 ó mas trabajadores"
plab <- "Población (censo 1970) en miles"
plot(popul ~ manu, data = polucion, xlab = mlab, ylab = plab)

rug(polucion$manu, side = 1) #Con “rayitas”


rug(polucion$popul, side = 2)

#Diagrama de dispersión Distribución marginal


layout(matrix(c(2, 0, 1, 3), nrow = 2, byrow = TRUE), widths = c(2, 1), heights = c(1, 2), respect = F)
xlim <- with(polucion, range(manu)) * 1.1
plot(popul ~ manu, data = polucion, cex.lab = 0.9,
xlab = mlab, ylab = plab, type = "n", xlim = xlim)
with(polucion, text(manu, popul, cex = 0.6,
labels = abbreviate(row.names(polucion))))
with(polucion, hist(manu, main = "", xlim = xlim))
with(polucion, boxplot(popul, main=""))
dev.off()

#Boxplot bivariado
library("MVA")
outcity <- match(lab <- c("Chicago", "Detroit", "Cleveland", "Philadelphia"), rownames(polucion))
x <- polucion[, c("manu", "popul")]
titulo <- "Boxplot Bivariado"
bvbox (x, main=titulo, xlab = mlab, ylab = plab)
text(x$manu[outcity], x$popul[outcity], labels = lab, cex = 0.7, pos = c(2, 2, 4, 2))

#BOXPLOT BIVARIADO: Calculando la correlación con todos los datos


with(polucion, cor(manu, popul))
[1] 0.9552693
# BOXPLOT BIVARIADO: Calculando la correlación excluyendo los atípicos
outcity <- match(c("Chicago", "Detroit", "Cleveland", "Philadelphia"), rownames(polucion))
with(polucion, cor(manu[-outcity], popul[-outcity]))
[1] 0.7955549
#Todos los datos que estén en la periferia de la nube de puntos tienen potencial de ser atípicos. En
consecuencia cambian las estimaciones y por ende el coeficiente de correlación.

#Envolvente convexa
> (hull <- with(polucion, chull(manu, popul)))
[1] 9 15 41 6 2 18 16 14 7
> rownames(polucion)

#Gráfico envolvente convexa


with (polucion, plot(manu, popul, pch = 1, xlab = mlab, ylab = plab))
with (polucion, polygon(manu[hull], popul[hull], density = 15, angle = 30))
#Correlación eliminado los datos de la envolvente convexa
with(polucion, cor(manu[-hull],popul[-hull]))
[1] 0.9225267

#Grafico burbuja – diagrama dispersión


rm(list=ls())
setwd("C:/Users/manuel.contento/Dropbox/Curso_AM/BasesDatos")
polucion <- read.table("polucionUSA.csv", header=TRUE, sep=";", dec=".", row.names="City")
#Diagrama de Dispersión
plot(wind ~ temp, data = polucion,xlab = "Temperatura promedio/anual (Fahrenheit)",
ylab = "Velocidad viento promedio/anual (m.p.h.)", pch = 1,
main="Diagrama de dispersión de temperatura y viento")

#Diagrama burbuja
ylim <- with(polucion, range(wind)) * c(0.95, 1)
plot(wind ~ temp, data = polucion, xlab = "Temperatura promedio/anual (Fahrenheit)",
ylab = "Velocidad viento promedio/anual (m.p.h.)", pch = 10, ylim = ylim,
main="Grafico de Burbuja de temperatura, viento y SO2")
with(polucion, symbols(temp, wind, circles = SO2,inches = 0.5, add = TRUE))

#Diagrama de dispersión de estrella (Scattelpot estrella)


plot(wind ~ temp, data = polucion, xlab = "Temperatura promedio/anual (Fahrenheit)",
ylab = "Velocidad viento promedio/anual (m.p.h.)", pch = 10, ylim = ylim)
with(polucion, stars(polucion[,-c(2,5)], locations = cbind(temp, wind),
labels = NULL, add = TRUE, cex = 0.5))

#Diagrama de estrella
stars(polucion, key.loc=c(-2,13),cex = 0.6)

#Matriz de dispersión
pairs(polucion, pch = ".", cex = 1.5)

pairs(polucion,panel = function (x, y, ...) {


points(x, y, ...)
abline(lm(y ~ x), col = "grey")
}, pch = ".", cex = 1.5)

S-ar putea să vă placă și