how do I find the mean value of one variable for every 5th percentile of another variable

Viewed 35

I want to find the mean of one numeric variable for each percentile of another numeric variable. To essentially replicate this graph (Marian et al(2012) but for my own data:

Figure 6. Average orthographic neighborhood size as a function of word frequency. Frequency bins are evenly spaced divisions of words in 5% increments. Bin one represents the average orthographic neighborhood size of the top 5% most frequent words in the language, bin twenty represents the average orthographic neighborhood size of the 5% least frequent words.

I have tried the following:

tapply(quantile(CLEARPOND$word_frequency, probs = c(.05, .10, .15, .20, .25,.30,.35,.40,.45,.50,.55,.60,.65,.70,.75,.80,.85,.90,.95)), CLEARPOND$Colthearts_N, mean)

which returns the following error:

Error in tapply(quantile(CLEARPOND$word_frequency, probs = c(0.5, 0.1, : arguments must have same length

Is there anyway to fix this/ do this in a more logical way?

I basically want to divide the variable word_frequency into bins of 5% increments. And then find the mean of Colthearts_N for each of those bins. I would also ideally like to plot this on a scatter plot.

My percentiles for word_frequency are as follows:

5% 10% 15% 20% 25% 30% 35% 40% 45% 50% 55% 60% 65% 70% 75% 80% 85% 90% 95% 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 3 4 6 11

Any help would be appreciated

1 Answers

I invented data to test the solution; I believe it achieves your goal

set.seed(42)
CLEARPOND <- data.frame(
  word_frequency = rnorm(1000),
  Colthearts_N = sample(1:100,
    size = 1000, replace = TRUE
  )
) %>% arrange(
  word_frequency
)

mutate(CLEARPOND,
  bin = cut(
    x = word_frequency,
    breaks = c(
      -Inf, quantile(word_frequency,
        probs = seq(from = 0.05, to = .95, by = .05)
      ),
      Inf
    )
  )
) |>
  group_by(bin) |>
  summarise(avg = mean(Colthearts_N),
            n= n())
Related