Applying a function to each quantile of an R dataframe

557 views Asked by At

I have an R dataframe and I want to apply an estimation function for each of its quantiles. Here's an example with lm():

df <- data.frame(Y = sample(100), X1 = sample(100), X2 = sample(100))
estFun <- function(df){lm(Y ~ X1 + X2, data = df)}

If I split that in two subsets on both sides of the median, I manage with two lines:

fitsLo <- estFun(df[df$Y < median(df$Y),])
fitsHi <- estFun(df[df$Y > median(df$Y),])

However, I would like to find a more general solution where I could arbitrarily choose the number of quantiles and perhaps with lapply() form a list of fits.

2

There are 2 answers

0
agstudy On BEST ANSWER

You can use findInterval in combination with by;

by(df,findInterval(df$Y,quantile(df$Y,c(0.25,0.5,0.75))),estFun)
0
akrun On

Try

 df$grp <- with(df, cut(Y, breaks=quantile(Y)))
 by(df, df$grp, FUN=estFun)