How to calculate the Mcloone index?

Asked

Viewed 36 times

-4

I have a database union where the following operation needs to be performed with the VA column, broken down by state (UF):

Divide the sum of all observations below the median by the median multiplied by the number of observations below the median.

I was only able to calculate the state-disaggregated median using the following code::

median <- aggregate(x=uniao$VAA, by=list (uniao$UF.x), FUN=median)

Then, I could not proceed. Follow a sample of the base. Thanks.

~Cod.IBGE, ~UF.x, ~GastoMunEduc2017,             ~Municipio,          ~VAA,
2926657L,  "BA",               293,    "RIBEIRAO DO LARGO", "0,112045889",
2708501L,  "AL",           5262048, "SAO LUIS DO QUITUNDE", "71,44668024",
3141108L,  "MG",         118502319,           "MATOZINHOS", "351,9522394",
1503309L,  "PA",        1265213922,         "IGARAPE-MIRI", "739,8046556",
2103802L,  "MA",         425038332,            "DOM PEDRO", "899,7424471",
2708105L,  "AL",         524661607,    "SANTANA DO MUNDAU", "2000,234872",
2807303L,  "SE",         117161813,                "TELHA", "2099,674068",
2407500L,  "RN",         598046562,         "MAXARANGUAPE", "2202,749768",
4303301L,  "RS",          82870338,              "CAIBATE", "2233,701833",
2108603L,  "MA",        4369992772,             "PINHEIRO", "2386,670001",
1304104L,  "AM",         960827383,               "TAPAUA", "2420,220108",

1 answer

0


For starters, the column VAA is in quotes and is therefore read as "character", it must first turn into "numeric".

uniao$VAA <- as.numeric(sub(",", ".", uniao$VAA))

To add the column VAA for UF.x you can use the formula interface.

aggregate(VAA ~ UF.x, uniao, median)

But it is not necessary to calculate this result to obtain the value described in the question.

fun <- function(x){
  med <- median(x)
  mean(x[x < med])/med
}

fun(uniao$VAA)
#[1] 0.2062816

If you want to aggregate the data by applying this function, it can be with an instruction very similar to the one above, just change median for fun.

aggregate(VAA ~ UF.x, uniao, fun)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.