# Instalamos los paquetes necesarios
install.packages(c("mice", "factoextra", "gridExtra"))
# Invocamos las librerías necesarias
library(mice)
library(factoextra)
library(gridExtra)
#leer los datos
airq_dt<-read.csv("airq_dt.csv",sep = ",",header = T)
#imputar valores perdidos con función de R
imp_dt<-mice(airq_dt[,-7],m=1,seed=46)
imp_dt$imp$Ozono
#data con valores imputados
completedData <- complete(imp_dt,1)
dim(completedData)
str(completedData)
completedData[4,]
########
#Análisis de componentes principales
pca_air <- prcomp(completedData, scale = TRUE) #scale=T estandariza
#media
pca_air$center
# standard deviations
pca_air$scale
##vectores
pca_air$rotation
#cambiar la dirección
pca_air$rotation <- -pca_air$rotation
pca_air$rotation
#los puntajes de los pc
pca_air$x <- - pca_air$x
head(pca_air$x)
#proporción de varianza explicada
VE <- pca_air$sdev^2
PVE <- VE / sum(VE)
round(PVE, 2)
summary(pca_air)
#gráficos de proporción de varianza
# PVE
PVEp<- fviz_eig(pca_air,geom="bar")
# Cumulative PVE plot
PVEa <- qplot(c(1:ncol(completedData)), cumsum(PVE)) +
geom_line() +
xlab("Principal Component") +
ylab("PVE-acumulado") +
# ggtitle("Cumulative Scree Plot") +
ylim(0,1)
grid.arrange(PVEp, PVEa, ncol = 2)
PC1 <- pca_air$rotation[,1]
PC1_scores <- abs(PC1)
PC1_scores_ordered <- sort(PC1_scores, decreasing = TRUE)
names(PC1_scores_ordered)
ggplot(completedData, aes(x=Temp, y=Ozono, color = as.factor(Mes))) +
geom_point()+
labs(x="Temperatura",color="Mes")
##resultados
# Eigenvalues
eig.val <- get_eigenvalue(pca_air)
eig.val
# resultado por variable
res.var <- get_pca_var(pca_air)
res.var$coord[,1:2] # coordenadas
res.var$contrib[,1:2] # contribucion a PC
res.var$cos2[,1:2] # calidad de representación
fviz_pca_var(pca_air,col.var = "contrib", gradient.cols = c("#00AFBB", "#E7B800",
"#FC4E07"),repel = TRUE)
##Regresión lineal simple
library(ggplot2)
library(ggpubr)
ggplot(completedData,aes(y=Ozono,x=Vient,add="reg.line"))+
stat_regline_equation(label.x = 10, label.y = 120)+
geom_smooth(method=lm, se=T)+
geom_point()+
#scale_y_continuous(limits = c(0,20))+
labs(x = "Ozono", y = "Viento")+
theme(text = element_text(size=14))+
theme_grey(base_size = 16)
lmod<-lm(Ozono~Vient,data = completedData)
summary(lmod)
coef(lmod) #estimación de coeficientes
confint(lmod,level = 0.95) #intervalo de confianza
completedData$resid<-lmod$residuals
completedData$obs<-1:length(completedData$resid)
summary(completedData$resid)
completedData$fit<-lmod$fitted.values
hist(completedData$resid,main = "Histograma residuos")
shapiro.test(completedData$resid)
plot(lmod,which = 2,col=c("blue"))
ggplot(completedData,aes(x=obs,y=resid))+
geom_point()+
geom_hline(yintercept = 0,linetype="dashed", color = "red")+
labs(x = "No. Observaciones", y = "Residuos")+
theme(text = element_text(size=14))+
theme_grey(base_size = 16)
ggplot(completedData,aes(x=Ozono,y=fit))+
geom_point()+
geom_line(aes(x=Ozono, y=Ozono),linetype="dashed",col=2)+
labs(x = "Ozono", y = "Ozono-estimadas")+
theme(text = element_text(size=14))+
theme_grey(base_size = 16)
#análisis de varianza
anova_ej1<-aov(Ozono~Vient,data = completedData)
summary(anova_ej1)
##########################
#Ejemplo 2
dat_ej2<-read.csv("ej2.csv",sep=",",header=T)
str(dat_ej2)
mod2<-lm(y~x1+x2+x3+x4,data = dat_ej2)
summary(mod2)
coef(mod2) #estimación coeficientes
confint(mod2)#intervalos de confianza
#gráfico Q-Q
plot(mod2,which = 2,col=c("blue"))
dat_ej2$resid<-mod2$residuals
shapiro.test(dat_ej2$resid)
dat_ej2$obs<-1:length(dat_ej2$resid)
summary(dat_ej2$resid)
ggplot(dat_ej2,aes(x=obs,y=resid))+
geom_point()+
geom_hline(yintercept = 0,linetype="dashed", color = "red")+
labs(x = "No. Observaciones", y = "Residuos")+
theme(text = element_text(size=14))+
theme_grey(base_size = 16)
dat_ej2$hat_y<-mod2$fitted.values
ggplot(dat_ej2,aes(x=y,y=hat_y))+
geom_point()+
geom_line(aes(x=y, y=y),linetype="dashed",col=2)+
labs(x = "Observaciones", y = "Predicciones")+
theme(text = element_text(size=14))+
theme_grey(base_size = 16)