####################################################################################################################### # Calcula las proporciones de cada uno de los niveles de los factores de un conjunto de columnas, y su error estandar # sdata: dataframe con los datos # cppc: vector de índices o nombres de columnas de tipo factor para las que se desean obtener las proporciones # wght: índice o nombre de la columna con el peso de cada fila # brr: vector de índices o nombres de las columnas con los pesos replicados # Devuelve una matriz con una columna para cada uno de los niveles de los factores. # La primera fila es para el procentaje de cada valor en la columna, la segunda es el error estandar ####################################################################################################################### # Computes the proportions for each level of the factors in a set of columns along with their standard errors # sdata: dataframe with the data # cppc: vector of indexes or names of columns with factor data type for which you want to obtain the proprotions # wght: index or name of the column with the row weight # brr: vector of indexes or names of columns with the replicate weights # Returns a matrix with one column for each level of the factors. # The first row contains the proportions of each value, the second is for the standard error wght_ppc<-function(sdata,cppc,wght,brr) { nc<-0; for (i in 1:length(cppc)) { nc <- nc + length(levels(as.factor(sdata[,cppc[i]]))); } mppc<-matrix(ncol=nc,nrow=2); mppc[,]<-0; cn<-c(); for (i in 1:length(cppc)) { for (j in 1:length(levels(as.factor(sdata[,cppc[i]])))) { if (is.numeric(cppc[i])) { cn<-c(cn, paste(names(sdata)[cppc[i]],levels(as.factor(sdata[,cppc[i]]))[j],sep="-")); } else { cn<-c(cn, paste(cppc[i],levels(as.factor(sdata[,cppc[i]]))[j],sep="-")); } } } colnames(mppc)<-cn; rownames(mppc)<-c("PPC","SE"); swght<-sum(sdata[,wght]); ix<-1; for (i in 1:length(cppc)) { for (j in 1:length(levels(as.factor(sdata[,cppc[i]])))) { rfact<-sdata[,cppc[i]]==levels(as.factor(sdata[,cppc[i]]))[j]; mppc[1,ix]<-sum(sdata[rfact,wght]) / swght; for (k in 1:length(brr)) { sbrr<-sum(sdata[,brr[k]]); ppcbrr<-sum(sdata[rfact,brr[k]]) / sbrr; mppc[2,ix]<-mppc[2,ix] + (ppcbrr-mppc[1,ix])^2; } ix<-ix + 1; } } mppc[2,]<-sqrt((mppc[2,] * 4) / length(brr)); return(mppc); } ####################################################################################################################### # Calcula las proporciones de cada uno de los niveles de los factores de un conjunto de columnas combinado con los niveles # de cada una de las otras, y su error estandar # sdata: dataframe con los datos # cppc: vector de índices o nombres de columnas de tipo factor para las que se desean obtener las proporciones # wght: índice o nombre de la columna con el peso de cada fila # brr: vector de índices o nombres de las columnas con los pesos replicados # Devuelve una matriz con una columna para cada una de las combinaciones de los valores de los factores. # La primera fila es para el procentaje de cada valor en la columna, la segunda es el error estandar ####################################################################################################################### # Computes the proportions for each level of the factors in a set of columns combined with the levels on each other, # along with their standard errors # sdata: dataframe with the data # cppc: vector of indexes or names of columns with factor data type for which you want to obtain the proprotions # wght: index or name of the column with the row weight # brr: vector of indexes or names of columns with the replicate weights # Returns a matrix with one column for each level of the factors. # The first row contains the proportions of each value, the second is for the standard error wght_ppccombined<-function(sdata,cppc,wght,brr) { nc<-1; for (i in 1:length(cppc)) { nc <- nc * length(levels(as.factor(sdata[,cppc[i]]))); } mppc<-matrix(ncol=nc,nrow=2); mppc[,]<-0; cn<-c(); ####################################################################################################################### # Para evitar calcular las combinaciones con un proceso recursivo, lo que requeriria otra función, # usaremos el vector ccom para ir definiendo las combinaciones. Cada posición del vector se corresponderá # con el factor correspondiente del vector cppc, y contendrá el ordinal de uno de los niveles del factor. # En cada vuelta del bucle, el vector ccom definirá una combinación diferente de los niveles de todos los factores en cppc ####################################################################################################################### # To avoid calculate the combinations in a recursive process, which would require another function, # we will use the ccom vector to define the combinations. Each position of the vector corresponds # with the corresponding factor in cppc, and contains the ordinal of one of the factor levels. # In each round of the loop, the ccom vector contains a different combination of the levels of all the factors in cppc ####################################################################################################################### ccom<-rep(1,length(cppc)); bw<-TRUE; while(bw) { cnc<-paste(names(sdata)[cppc[1]],levels(as.factor(sdata[,cppc[1]]))[ccom[1]],sep="-"); for (i in 2:length(ccom)) { if (is.numeric(cppc[i])) { cnc<-paste(cnc,names(sdata)[cppc[i]],levels(as.factor(sdata[,cppc[i]]))[ccom[i]],sep="-"); } else { cnc<-paste(cnc,cppc[i],levels(as.factor(sdata[,cppc[i]]))[ccom[i]],sep="-"); } } cn<-c(cn, cnc); for (i in length(cppc):1) { if (ccom[i] < length(levels(as.factor(sdata[,cppc[i]])))) { ccom[i] = ccom[i] + 1; break; } else { if (i == 1) { bw<-FALSE; break; } else { ccom[i] = 1; } } } } colnames(mppc)<-cn; rownames(mppc)<-c("PPC","SE"); swght<-sum(sdata[,wght]); ix<-1; ####################################################################################################################### # volvemos a realizar el procedimiento anterior para procesar las diferentes combinaciones de niveles de los factores ####################################################################################################################### # perform again the previous procedure for calculate the combinations of the factor levels ####################################################################################################################### bw<-TRUE; ccom<-rep(1,length(cppc)); while(bw) { rfact<-sdata[,cppc[1]]==levels(as.factor(sdata[,cppc[1]]))[ccom[1]]; for (i in 2:length(ccom)) { rfact<-rfact & (sdata[,cppc[i]]==levels(as.factor(sdata[,cppc[i]]))[ccom[i]]); } mppc[1,ix]<-sum(sdata[rfact,wght]) / swght; for (k in 1:length(brr)) { sbrr<-sum(sdata[,brr[k]]); ppcbrr<-sum(sdata[rfact,brr[k]]) / sbrr; mppc[2,ix]<-mppc[2,ix] + (ppcbrr-mppc[1,ix])^2; } ix<-ix + 1; for (i in length(cppc):1) { if (ccom[i] < length(levels(as.factor(sdata[,cppc[i]])))) { ccom[i] = ccom[i] + 1; break; } else { if (i == 1) { bw<-FALSE; break; } else { ccom[i] = 1; } } } } mppc[2,]<-sqrt((mppc[2,] * 4) / length(brr)); return(mppc); } ####################################################################################################################### # Calcula las proporciones de todas las combinaciones de los niveles de dos grupos de factores cruzados y sus errores estandar # sdata: dataframe con los datos # rppc: vector con los índices o nombres dee las columnas con los factores para las filas # cppc: vector con los índices o nombres dee las columnas con los factores para las columnas # wght: índice o nombre de la columna con el peso de cada fila # brr: vector de índices o nombres de las columnas con los pesos replicados # Devuelve una lista con dos matrices, la primera con las proporciones y la segunda con los errores estandar ####################################################################################################################### # Computes the proportions of all the combinations obtained by crossing the levels of two sets of factors, and their standard errors # sdata: dataframe with the data # rppc: vector of indexes or names of columns with factor data type for the rows # cppc: vector of indexes or names of columns with factor data type for the columns # wght: index or name of the column with the row weight # brr: vector of indexes or names of columns with the replicate weights # Returns a list with two matrixes, the former with the proportions and the other with the standard errors wght_ppccrossed<-function(sdata,rppc,cppc,wght,brr) { nc<-0; for (i in 1:length(cppc)) { nc <- nc + length(levels(as.factor(sdata[,cppc[i]]))); } nr<-0; for (i in 1:length(rppc)) { nr <- nr + length(levels(as.factor(sdata[,rppc[i]]))); } mppc<-matrix(ncol=nc,nrow=nr); mppc[,]<-0; seppc<-matrix(ncol=nc,nrow=nr); seppc[,]<-0; result<-vector('list',2); names(result)<-c("PPC","SE"); cn<-c(); for (i in 1:length(cppc)) { for (j in 1:length(levels(as.factor(sdata[,cppc[i]])))) { if (is.numeric(cppc[i])) { cn<-c(cn, paste(names(sdata)[cppc[i]],levels(as.factor(sdata[,cppc[i]]))[j],sep="-")); } else { cn<-c(cn, paste(cppc[i],levels(as.factor(sdata[,cppc[i]]))[j],sep="-")); } } } colnames(mppc)<-cn; colnames(seppc)<-cn; cr<-c(); for (i in 1:length(rppc)) { for (j in 1:length(levels(as.factor(sdata[,rppc[i]])))) { if (is.numeric(rppc[i])) { cr<-c(cr, paste(names(sdata)[rppc[i]],levels(as.factor(sdata[,rppc[i]]))[j],sep="-")); } else { cr<-c(cr, paste(rppc[i],levels(as.factor(sdata[,rppc[i]]))[j],sep="-")); } } } rownames(mppc)<-cr; rownames(seppc)<-cr; ir<-1; swght<-sum(sdata[,wght]); for (r in 1:length(rppc)) { for (l in 1:length(levels(as.factor(sdata[,rppc[r]])))) { rfact <- (sdata[,rppc[r]]==levels(as.factor(sdata[,rppc[r]]))[l]); ic<-1; for (i in 1:length(cppc)) { for (j in 1:length(levels(as.factor(sdata[,cppc[i]])))) { cfact<-(sdata[,cppc[i]]==levels(as.factor(sdata[,cppc[i]]))[j]) & rfact; mppc[ir,ic]<-sum(sdata[cfact,wght]) / swght; for (k in 1:length(brr)) { sbrr<-sum(sdata[,brr[k]]); ppcbrr<-sum(sdata[cfact,brr[k]]) / sbrr; seppc[ir,ic]<-seppc[ir,ic] + (ppcbrr-mppc[ir,ic])^2; } ic<-ic + 1; } } ir<-ir + 1; } } seppc[,]<-sqrt((seppc[,] * 4) / length(brr)); result[[1]]<-mppc; result[[2]]<-seppc; return(result); } ####################################################################################################################### # Calcula las proporciones de todos los niveles de un grupo de factores agrupados por país y sus errores estandar # sdata: dataframe con los datos # cnt: índice o nombre de la columna con el país # cppc: vector con los índices o nombres dee las columnas con los factores para las columnas # wght: índice o nombre de la columna con el peso de cada fila # brr: vector de índices o nombres de las columnas con los pesos replicados # Devuelve una lista con dos matrices, la primera con las proporciones, con una fila por paía, y la segunda con los errores estandar ####################################################################################################################### # Computes the proportions of all the levels of a set of factors, grouped by country, and their standard errors # as the previous example, but in this case considering each row as a separated data set # sdata: dataframe with the data # cnt: index or name of the column with the country # cppc: vector of indexes or names of columns with factor data type for the columns # wght: index or name of the column with the row weight # brr: vector of indexes or names of columns with the replicate weights # Returns a list with two matrixes, the former with the proportions, with a row by country, and the other with the standard errors wght_ppc_bycnt<-function(sdata,cnt,cppc,wght,brr) { nc<-0; for (i in 1:length(cppc)) { nc <- nc + length(levels(as.factor(sdata[,cppc[i]]))); } nr<-length(levels(as.factor(sdata[,cnt]))); mppc<-matrix(ncol=nc,nrow=nr); mppc[,]<-0; seppc<-matrix(ncol=nc,nrow=nr); seppc[,]<-0; result<-vector('list',2); names(result)<-c("PPC","SE"); cn<-c(); for (i in 1:length(cppc)) { for (j in 1:length(levels(as.factor(sdata[,cppc[i]])))) { if (is.numeric(cppc[i])) { cn<-c(cn, paste(names(sdata)[cppc[i]],levels(as.factor(sdata[,cppc[i]]))[j],sep="-")); } else { cn<-c(cn, paste(cppc[i],levels(as.factor(sdata[,cppc[i]]))[j],sep="-")); } } } colnames(mppc)<-cn; colnames(seppc)<-cn; cr<-c(); for (j in 1:length(levels(as.factor(sdata[,cnt])))) { cr<-c(cr, levels(as.factor(sdata[,cnt]))[j]); } rownames(mppc)<-cr; rownames(seppc)<-cr; ir<-1; for (l in 1:length(levels(as.factor(sdata[,cnt])))) { rfact <- (sdata[,cnt]==levels(as.factor(sdata[,cnt]))[l]); swght<-sum(sdata[rfact,wght]); ic<-1; for (i in 1:length(cppc)) { for (j in 1:length(levels(as.factor(sdata[,cppc[i]])))) { cfact<-(sdata[,cppc[i]]==levels(as.factor(sdata[,cppc[i]]))[j]) & rfact; mppc[ir,ic]<-sum(sdata[cfact,wght]) / swght; for (k in 1:length(brr)) { sbrr<-sum(sdata[rfact,brr[k]]); ppcbrr<-sum(sdata[cfact,brr[k]]) / sbrr; seppc[ir,ic]<-seppc[ir,ic] + (ppcbrr-mppc[ir,ic])^2; } ic<-ic + 1; } } ir<-ir + 1; } seppc[,]<-sqrt((seppc[,] * 4) / length(brr)); result[[1]]<-mppc; result[[2]]<-seppc; return(result); }