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.
You can use
findInterval
in combination withby
;