I am learning R programming by way of reviewing basic stats with Julian Faraway's book "Linear Models with R". Very early in the book he gives example code to implement a Permutation Test on one of his datasets (included in the "faraway" package).
Am I correct that using a for() looping construct in this manner is a poor habit to get into when programming in R?
In theory I'd think the sort of thing he's doing is best implemented with one of the apply-family functions. But for some reason I seem to have a total mental block about how exactly one passes parameters into and out of a non-built-in function supplied to the apply().
Here's the way he does it. I just spent an hour-plus looking at various examples of apply() and can't seem to get my mind around it. Not sure what it is about apply() that I find so opaque.
library(faraway)
library(tidyverse)
data(gala,package="faraway")
lmod <- lm( Species ~ Nearest + Scruz, gala)
lms<-summary(lmod)
nreps<-4000
set.seed(123)
fstats<-numeric(nreps)
for(i in 1:nreps){
lmods<-lm(sample(Species)~Nearest+Scruz,gala)
fstats[i]<-summary(lmods)$fstat[1]
}
mean(fstats > lms$fstat[1])
#> [1] 0.55825
Created on 2018-12-09 by the reprex package (v0.2.1.9000)
Here's where I was when I finally gave up on writing my own version, after a couple dozen variations and attempts...
fstats<-apply(fstats,1,function(x) summary(lm(sample(Species)~Nearest+Scruz,gala))$fstat[1])
mean(fstats > lms$fstat[1])