Llamo a las librerias que necesito,leo los archivos, creo el dataframe que necesito, lo limpio (quitarle 0 y NA), filtro las columnas que necesito y después divido el dataframe acorde a nombres comunes en ellas, y todo eso funciona:
library(fitdistrplus)
library(MASS)
library(survival)
library(tidyverse)
library(ggplot2)
library(actuar)
library(e1071)
library(FAdist)
library(gld)
library(MonteCarlo)
library(snow)
archivos <- c("2014_1.csv",
"2014_2.csv",
"2014_3.csv",
"2014_4.csv",
"2015_1.csv",
"2015_2.csv",
"2015_3.csv",
"2015_4.csv",
"2016_1.csv",
"2016_2.csv",
"2016_3.csv",
"2016_4.csv",
"2017_1.csv",
"2017_2.csv",
"2017_3.csv",
"2017_4.csv",
"2018_1.csv",
"2018_2.csv",
"2018_3.csv",
"2018_4.csv",
"2019_1.csv")
lista_df <- lapply(archivos, function (x) read.table(x, sep=";",header=T))
df_unido <- reduce(rbind, lista_df)
df_unido_n <- df_unido %>%
dplyr::select(NombreCentral,POTENCIA_BRUTA_MWH,COMBUSTIBLE,CONCENTRACION_CO2_TON_MWH) %>%
filter(POTENCIA_BRUTA_MWH >0,!is.na(POTENCIA_BRUTA_MWH) , CONCENTRACION_CO2_TON_MWH >0, !is.na(CONCENTRACION_CO2_TON_MWH))
df_unido_n$CONCENTRACION_CO2_TON = df_unido_n$CONCENTRACION_CO2_TON_MWH * df_unido_n$POTENCIA_BRUTA_MWH
lista_total= list()
for (i in 1:44){
dfn=df_unido_n %>%
filter(NombreCentral == nmb[i])
lista_total[[i]]<-dfn
}
Todo dentro de la función funciona, lo evalué para algunos elementos de lista_total y corre sin ningún problema. Grafico para ver comportamiento de los datos (esta parte es la que falla), creo variables para aproximar funciones de distribución a los datos, vuelvo a graficar, pero esta vez con las distribuciones para ver como se ajustan a los datos (esta parte también falla) y finalmente veo que tan certero son mis aproximaciones (summary, gofstaty bootdist, este último es mas lento que el resto)
function.CONCENTRACION_CO2_TON<-function(var_list){
ggplot(var_list, aes(x = CONCENTRACION_CO2_TON)) + geom_density()
ggplot(var_list, aes(var_list$CONCENTRACION_CO2_TON)) + geom_histogram(binwidth = 20)
ggplot(var_list, aes(var_list$CONCENTRACION_CO2_TON)) + stat_ecdf(geom = "point", size=1) + ggtitle("CDF GRAPHIC")
ggplot(var_list, aes(x = CONCENTRACION_CO2_TON)) + geom_histogram(aes(y=..density..), binwidth=0.1, colour="black", fill="white") + geom_density(alpha=0.2, size=0.4) + ggtitle("DENSITY AND HISTOGRAM GRAPHIC")
col_1=var_list$CONCENTRACION_CO2_TON
print(summary(col_1))
plotdist(col_1, histo=TRUE, demp=TRUE)
descdist(col_1)
fw2_1 <- fitdist(col_1, "weibull")
fw3_1 <- fitdist(col_1, "weibull3", start = list(shape = 1, scale = 1))
fg_1 <- fitdist(col_1, "gamma")
fln_1 <- fitdist(col_1, "lnorm")
fex_1 <- fitdist(col_1, "exp")
fgm_1 <- fitdist(col_1, "gumbel",start=list(scale=50, location=50))
fn_1 <- fitdist(col_1, "norm")
fll_1 <- fitdist(col_1, "llogis", start = list(shape = 1, scale = 1))
fl_1 <- fitdist(col_1, "logis")
par(mfrow=c(2,2))
plot.legend<-c("Weibull","Weibull3","lnorm","gamma","exp","gumbel","norm","llogis","logis")
denscomp(list(fw2_1,fw3_1,fg_1,fln_1,fex_1,fgm_1,fn_1,fll_1,fl_1), plotstyle = "ggplot",legendtext = c("weibull-2P", "weibull-3P", "gamma", "lognormal", "exponential", "gumbel", "normal", "loglogistic", "logistic"))
cdfcomp(list(fw2_1,fw3_1,fg_1,fln_1,fex_1,fgm_1,fn_1,fll_1,fl_1), plotstyle = "ggplot",legendtext = c("weibull-2P", "weibull-3P", "gamma", "lognormal", "exponential", "gumbel", "normal", "loglogistic", "logistic"))
qqcomp(list(fw2_1,fw3_1,fg_1,fln_1,fex_1,fgm_1,fn_1,fll_1,fl_1), plotstyle = "ggplot",legendtext = c("weibull-2P", "weibull-3P", "gamma", "lognormal", "exponential", "gumbel", "normal", "loglogistic", "logistic"))
ppcomp(list(fw2_1,fw3_1,fg_1,fln_1,fex_1,fgm_1,fn_1,fll_1,fl_1), plotstyle = "ggplot",legendtext = c("weibull-2P", "weibull-3P", "gamma", "lognormal", "exponential", "gumbel", "normal", "loglogistic", "logistic"))
print(summary(fw2_1))
print(summary(fw3_1))
print(summary(fg_1))
print(summary(fln_1))
print(summary(fex_1))
print(summary(fgm_1))
print(summary(fn_1))
print(summary(fll_1))
print(summary(fl_1))
print(gofstat(fw2_1))
print(gofstat(fw3_1))
print(gofstat(fg_1))
print(gofstat(fln_1))
print(gofstat(fex_1))
print(gofstat(fgm_1))
print(gofstat(fn_1))
print(gofstat(fll_1))
print(gofstat(fl_1))
print(bootdist(fw2_1,bootmethod = "param",niter=51,silent = TRUE))
print(bootdist(fw3_1,bootmethod = "param",niter=51,silent = TRUE))
print(bootdist(fg_1,bootmethod = "param",niter=51,silent = TRUE))
print(bootdist(fln_1,bootmethod = "param",niter=51,silent = TRUE))
print(bootdist(fex_1,bootmethod = "param",niter=51,silent = TRUE))
print(bootdist(fgm_1,bootmethod = "param",niter=51,silent = TRUE))
print(bootdist(fn_1,bootmethod = "param",niter=51,silent = TRUE))
print(bootdist(fll_1,bootmethod = "param",niter=51,silent = TRUE))
print(bootdist(fl_1,bootmethod = "param",niter=51,silent = TRUE))
}
Con lapply le aplico a lista_total (lista de 44 dataframes de distinta longitud cada uno)
col1t<-lapply(lista_total,function.CONCENTRACION_CO2_TON)
No sé que hago mal, porfa ayuda estimables. (Creo que hay esta parte de los datos) structure(list(NombreCentral = structure(c(28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L), .Label = c("ANDINA", "ANGAMOS", "ANTILHUE TG", "BOCAMINA ", "CANDELARIA", "CARDONES (EX TIERRA AMARILLA)", "CENTRAL ATACAMA", "CENTRAL ESPERANZA", "COLMITO", "CORONEL", "DIEGO DE ALMAGRO ", "EMELDA", "GUACOLDA", "HORCONES", "HUASCO", "IQUIQUE", "LAGUNA VERDE", "LOS PINOS", "LOS VIENTOS", "MEJILLONES", "NEHUENCO", "NORGENER", "QUINTERO ", "RENCA - NUEVA RENCA", "SAN FRANCISCO DE MOSTAZAL", "SAN ISIDRO I", "SAN ISIDRO II", "SAN LORENZO DE DIEGO DE ALMAGRO", "SANTA LIDIA", "SANTA MARÍA I", "TALTAL ", "TARAPACA", "TOCOPILLA", "VENTANA II", "VENTANA IV (EX CENTRAL CAMPICHE)", "VENTANAS I", "VENTANAS III", "YUNGAY (EX CAMPANARIO)", "EL SALVADOR", "LAUTARO-COMASA", "CENTRAL LOS GUINDOS", "CENTRAL TERMOELÉCTRICA COCHRANE", "CENTRAL KELAR", "SALAR", "TRES PUENTES"), class = "factor"), POTENCIA_BRUTA_MWH = c(22.1200008392334, 22.1200008392334, 22.1200008392334, 22.1200008392334, 22.1200008392334, 9.85000038146973, 9.85000038146973, 9.85000038146973, 9.85000038146973, 0.499000012874603, 1.47099995613098, 2.76300001144409, 4.68599987030029, 4.56899976730347, 13.58899974823, 8.62199974060059, 2.1340000629425, 6.85599994659424, 4.80499982833862, 4.56400012969971, 0.497000008821487, 9.79899978637695, 26.0629997253418, 12.8699998855591, 26.5599994659424, 29.5799999237061, 29.2199993133545, 28.8649997711182, 28.7199993133545, 28.7199993133545, 11.3100004196167, 8.09000015258789, 2.74000000953674, 11.9200000762939, 26.0499992370605, 26.2600002288818, 26.2849998474121, 26.2600002288818, 26.2450008392334, 13.75, 1.61000001430511, 0.194999992847443, 2.99000000953674, 20.6599998474121, 30, 30.4200000762939, 30.5599994659424, 30.5, 31.7600002288818, 30.5), CONCENTRACION_PORCENTAJE_CO2 = c(0.455000013113022, 0.446000009775162, 0.551999986171722, 0.528999984264374, 1.98399996757507, 0.328999996185303, 0.319000005722046, 0.225999996066093, 0.363999992609024, 0.0879999995231628, 0.279000014066696, 0.229000002145767, 0.356000006198883, 0.10700000077486, 0.143999993801117, 0.108999997377396, 0.202999994158745, 0.025000000372529, 0.546000003814697, 0.303000003099442, 0.250999987125397, 0.796000003814697, 1.33299994468689, 1.00399994850159, 4.92899990081787, 5.53499984741211, 5.42999982833862, 5.3730001449585, 5.36299991607666, 5.34000015258789, 6.59600019454956, 1.37300002574921, 1.14900004863739, 2.24300003051758, 4.69899988174438, 4.75600004196167, 4.77299976348877, 4.76399993896484, 4.76800012588501, 4.5149998664856, 0.467999994754791, 0.0710000023245811, 0.556999981403351, 3.59999990463257, 5.69999980926514, 5.80200004577637, 5.82100009918213, 5.84299993515015, 6.04699993133545, 5.7810001373291)), row.names = c(NA, 50L), class = "data.frame")