R/lmfit.R

Defines functions lmfit

#' Function for computing beta diversity within islands or plots
#'
#' @ indat = input data frame
#' @ group = grouping factor i.e. island or plot or whatever else
#' @ comp = character vector indicating column name identifying components of beta diversity
#'

lmfit<-function(indat,group,comp,hab){
    results<-NULL
    # unique list of islands
    site.l<-as.character(unique(indat[,group]))
    for (i in 1:length(site.l)){
        # subset island
        tmp<-indat[indat[,group] %in% site.l[i],]
        # unique list of components
        comp.l<-as.character(unique(tmp[,comp]))
        for (y in 1:length(comp.l)){
            tmp1<-tmp[tmp[,comp] %in% comp.l[y],]
            # unique list of habitats
            hab.l<-as.character(unique(tmp1[,hab]))
            for (z in 1:length(hab.l)){
                tmp2<-tmp1[tmp1[,hab] %in% hab.l[z],]
                # fit model
                mod<-lm(beta~distance,data=tmp2)
                res<-data.frame(site=site.l[i],comp=comp.l[y],hab=hab.l[z],lmres=paste("Slope = ",
                     round(summary(mod)$coefficients[,1][2],digits=3),
                    " \n"," R2 =",round(summary(mod)$r.squared,digits=2),sep=""),
                    slope=round(summary(mod)$coefficients[,1][2],digits=3))
                res$lmres<-as.character(res$lmres)
                results<-rbind(res,results)
            }
        }
    }
    return(results)
}
marcog77/Rmacdiv documentation built on May 9, 2018, 8:38 p.m.