lundi 4 mars 2019

condition in loop in R

I have a relative simple question for which I was not able to apply solutions I have found on the internet. Let's say we have:

set.seed(20)

data <- data.frame(month = rep(month.name, 25), 
a = rnorm(300, 0, 1), b = runif(300, 0, 7.2))

I want to calculate using a loop the f-test for variance between columns a and b for each month in month. This I done by using:

# create some empty vectors to fill in later
pval <- as.double()
ftest <- as.double()
month <- as.character()

for (i in unique(data$month)){
  print(i)
  # sh.1 <- shapiro.test(data$b[data$month==i])
  # sh.1[2] > 0.05 # apply log if it smaller than 0.05
  # sh.2 <- shapiro.test(data$b[data$month==i])
  # sh.2[2] > 0.05 # apply log if it smaller than 0.05
  var.t <- var.test(data$a[data$month==i], data$b[data$month==i])
  f <- round(var.t[[1]],2)
  p <- round(var.t$p.value,2)
  ftest <- append(ftest, f)
  pval <- append(pval, p)
  month <- append(month, i)
}

However, as far as I know, f-test is very sensitive to normal distribution. Therefore, I am planning to use a condition into loop where in case that p-value of shapiro test is smaller than 0.05 a log transformation for the data will be required; then it will be used into f-test.

Normally, I would to this with an ifelse condition but I am not very sure how to use it here. Any help here please?

Aucun commentaire:

Enregistrer un commentaire