booma <- scan(nlines = 1) 150 151 125 126 124 116 92 119 145 140 99 76 119 88 137 74 151 66 118 151 boomb <- scan(nlines = 2) 133 152 184 129 144 111 136 140 143 115 119 161 88 159 125 99 114 112 123 118 151 150 123 diff <- mean(booma) - mean(boomb) diff boom <- c(booma, boomb) n_perm <- 1000 # n_perm should probably be set to at least 5000 or 10000 permdiff = NULL # this sets up an "empty variable". All this does is let R know that "permdiff" # exists, but it tells R to keep it empty (otherwise R would give us errors # below when we refer to "permdiff" for (i in 1 : n_perm) { # This sets up the loop. "for" says" do everything after the "{", # letting "i" equal 1 to n_perm. For example, with n_perm = 1000, # this will run through the loop 1000 times. newboom <- sample(boom, length(boom)) # Basically what this does is re-arrange the combined sample (step 3). # We tell "sample" to sample from "boom", and to take a sample # that's the same length as "boom". All this does is re-arrange # our sample. aboom <- newboom[1 : length(booma)] bboom <- newboom[(length(booma) + 1) : length(boom)] # Now we take this re-arranged sample (newboom) and divide it into two # samples, each of which is the same length as the original samples. # # Note that the first sample is obviously of length "booma", and the # second sample is then everything else (from "booma + 1" to "boom") # permdiff[i] <- mean(aboom) - mean(bboom) # Calculates the difference in the means of our "new" samples. # We will have n_perm of these differences (e.g, if n_perm = 1000, # then we will have 1000 differences) } ndiffs <- sum(abs(permdiff) >= abs(diff)) # This is a logical expression. It is true if |permdiff| >= |diff|, and # then has the value 1. So for every difference that we get from our loop # that is bigger than our observed difference, we count "1". # We can look at it by doing: ndiffs p <- ndiffs/n_perm # We just figure out the proportion of our sampled differences # that are greater than our observed difference. p