[R-sig-eco] predicting species abundance
Sarah Goslee
sarah.goslee at gmail.com
Thu Feb 23 19:40:07 CET 2012
Take a look at this:
> i <- 1
> species.i <- species[i]
> dune$species.i
NULL
> dune[, species.i]
[1] 3 0 2 0 0 0 0 2 0 0 2 0 0 2 2 0 0 0 0 0
You need to change the way you're selecting columns from the dune
data frame.
Sarah
On Thu, Feb 23, 2012 at 1:28 PM, Vincenzo Ellis
<vincenzoaellis at gmail.com> wrote:
> Hi everyone,
>
> I was wondering if I could get some help with a code I'm working on, which
> is not working. I'm trying to predict species abundances within a
> community by using the first axis of a principal component analysis of the
> community leaving out the species of interest. The hypothesis being that
> species abundances should be well predicted by other species in the
> community.
>
> My code is as follows using the dune dataset from the vegan package:
>
> library(vegan)
> data(dune)
>
> finaldata <- data.frame(a= 0, b= 0, c= 0) #empty dataframe to put data into
> species <- colnames(dune)
>
> for (i in 1: 30) {
> species.i <- species[i]
> community.i <- dune
> community.i$species.i <- NULL
> comm.i.hel <- decostand(community.i, "hellinger")
> com.pca.i <- rda(comm.i.hel) # pca on community without species i
> site.score <- com.pca.i$CA$u
> site.score.pc1 <- site.score[, 1]
> spe.site.i <- dune$species.i
> predict.i <- summary(lm(spe.site.i~ site.score.pc1)) # regress pc1
> against species i abund at each site
> r.sq.i <- predict.i$r.squared
> p.val.i <- predict.i$p.value
> finaldata[i,] <- cbind(species.i, r.sq.i, p.val.i) #store relevant info
> for each species in the empty dataframe
> }
>
> When the loop gets to the linear regression part it gives an error saying
> that spe.site.i is NULL. I guess that's because I assigned it as a NULL
> variable. Does anyone know how to fix this? The code is not making the
> species.i null, it's making a new variable called species.i and making that
> null, I think.
>
> Any comments or suggestions would be appreciated. Thank you so much!
>
> Vincenzo
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-sig-ecology
mailing list