Question

I am undertaking spectral analysis on vegetation and am looking into seeing which is the most significant wavelength in order to undertake some remote sensing.

I have 2000 wavelengths however, so I need to find a way of running an ANOVA quickly and I have read about a 4 loop (or just loop) which means I could undertake this quicker, without having to do each one individually.

At the moment my R code is:

mod_structure1 <- lm(X350 ~ structure, data=mydata)   
mod_structure1
summary(mod_structure1)
boxplot(X350 ~ structure, data=mydata, xlab="blah", ylab="350nm")
anova(mod_structure1)

With X350 being a wavelength of 350nm.

I would really appreciate some help - my R knowledge isn't great so apologies if this doesn't really make sense.

No correct solution

OTHER TIPS

lapply is faster than for loop and does the same trick:

First define a vector with names of your wavelengths. Doing this precisely is difficult, because you did not include example data.

wavs <- c("X350", "X...", "X...",...)

As you have so many wavelengths you probably do not want to do this manually. Include an example of your data, so it's easier to advice, but say you had them as columns of a data frame (guessing from X350):

require(reshape2)
wavdata <- melt(mydata, id = "structure")
wavs <- levels(wavdata$variable)

Then you can go ahead and do the loop:

mods <- lapply(wavs, function(i) {
x <- subset(wavdata, variable == wavs)
mod_structure1 <- lm(variable ~ value, data=x)
anova(mod_structure1)})

This gives you an incredible list of ANOVAs, so be prepared. It might be better to get relevant parameters from those ANOVAs to a data.frame, which is then easier to handle.

mod.data <- lapply(wavs, function(i) {
x <- subset(wavdata, variable == wavs)
mod_structure1 <- lm(variable ~ value, data=x)
tmp <- anova(mod_structure1)
data.frame(Df = tmp$Df[[1]], F = tmp$F[[1]], p = tmp$P[[1]])
})

do.call(rbind, mod.data)

The code is not tested and might contain errors. However, you can generate your own from the idea behind it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top