1

I have coded following function:

one_way_anova <- function(m, n, sample_means, sample_vars) {
  keskiarvo = 1/m*sum(sample_means)
  otosv = (sum((sample_means-keskiarvo)^2))/(m-1)
  TS = (n*otosv)/(sum(sample_vars)/m)
  parvo = 1-pf(TS, m-1, m*(n-1))
  return(parvo)
}

And using following data:

set.seed(1)
dat <- matrix(rnorm(300*20), nrow=300)
sample_means <- matrix(rowMeans(dat), nrow=100)
sample_vars <- matrix(apply(dat, 1, var), nrow=100)
m <- nrow(sample_means)
n <- ncol(sample_means)

Now I try to use apply -function to calculate "parvo" with my function one_way_anova for dataset sample_means by individual rows with three samples (matrix is 100x3). apply(sample_means, 1, one_way_anova)

Which gives following error Error in FUN(newX[, i], ...) : argument "sample_means" is missing, with no default

1
  • perhaps this: apply(sample_means, 1, function(x) one_way_anova(x, m, n, sample_vars)) Commented May 15, 2020 at 13:10

1 Answer 1

4

Since your function one_way_anova needs multiple arguments, you need to pass all other arguments besides sample_means if you used apply.

If you want to run it over rows in sample_means and sample_vars, maybe you can try sapply like below

sapply(1:m,function(k) one_way_anova(m,n,sample_means[k,],sample_vars[k,]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.