189 8069 5689

R语言是怎么做方差分解的

本篇内容介绍了“R语言是怎么做方差分解的”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联建站是一家以网站建设公司、网页设计、品牌设计、软件运维、seo优化、小程序App开发等移动开发为一体互联网公司。已累计为成都搅拌罐车等众行业中小客户提供优质的互联网建站和软件开发服务。

 首先是读入数据
datatotal<-read.table("datasetmultifunctionality.txt", header=T, sep="\t")
colnames(datatotal)
   接下来的代码是对数据进行转化

有的是常规的标准化

有的是log转化

常规的标准化开头提到的推文里介绍了方差分解必须用标准化后的数据,但是有的log转化是什么意思呢?

#####logtransformation moments
datatotal[,c(12,13,16,17)]<-log(datatotal[,c(12,13,16,17)])
datatotal[,14]<-log(datatotal[,14]-min(datatotal[,14])+1)
datatotal[,15]<-log(datatotal[,15]-min(datatotal[,15])+1)
datatotal[,18]<-log(datatotal[,18]-min(datatotal[,18])+1)
datatotal[,19]<-log(datatotal[,19]-min(datatotal[,19])+1)

#####Zscorring environmental variables
datatotal$ELEVATION<-(datatotal$ELEVATION-mean(datatotal$ELEVATION))/sd(datatotal$ELEVATION)
datatotal$LAT<-(datatotal$LAT-mean(datatotal$LAT))/sd(datatotal$LAT)
datatotal$SINLONG<-(datatotal$SINLONG-mean(datatotal$SINLONG))/sd(datatotal$SINLONG)
datatotal$COSLONG<-(datatotal$COSLONG-mean(datatotal$COSLONG))/sd(datatotal$COSLONG)
datatotal$SLO<-(datatotal$SLO-mean(datatotal$SLO))/sd(datatotal$SLO)
datatotal$ARIDITY<-(datatotal$ARIDITY-mean(datatotal$ARIDITY))/sd(datatotal$ARIDITY)
datatotal$SAND<-(datatotal$SAND-mean(datatotal$SAND))/sd(datatotal$SAND)
datatotal$PH<-(datatotal$PH-mean(datatotal$PH))/sd(datatotal$PH)
datatotal$SR<-(datatotal$SR-mean(datatotal$SR))/sd(datatotal$SR)

#####Zscorring moments
datatotal$CWM_logH<-(datatotal$CWM_logH-mean(datatotal$CWM_logH))/sd(datatotal$CWM_logH)
datatotal$CWV_logH<-(datatotal$CWV_logH-mean(datatotal$CWV_logH))/sd(datatotal$CWV_logH)
datatotal$CWS_logH<-(datatotal$CWS_logH-mean(datatotal$CWS_logH))/sd(datatotal$CWS_logH)
datatotal$CWK_logH<-(datatotal$CWK_logH-mean(datatotal$CWK_logH))/sd(datatotal$CWK_logH)
datatotal$CWM_logSLA<-(datatotal$CWM_logSLA-mean(datatotal$CWM_logSLA))/sd(datatotal$CWM_logSLA)
datatotal$CWV_logSLA<-(datatotal$CWV_logSLA-mean(datatotal$CWV_logSLA))/sd(datatotal$CWV_logSLA)
datatotal$CWS_logSLA<-(datatotal$CWS_logSLA-mean(datatotal$CWS_logSLA))/sd(datatotal$CWS_logSLA)
datatotal$CWK_logSLA<-(datatotal$CWK_logSLA-mean(datatotal$CWK_logSLA))/sd(datatotal$CWK_logSLA)

#####Zscorring ecosystem functions

datatotal$BGL<-(datatotal$BGL-mean(datatotal$BGL))/sd(datatotal$BGL)
datatotal$FOS<-(datatotal$FOS-mean(datatotal$FOS))/sd(datatotal$FOS)
datatotal$AMP<-(datatotal$AMP-mean(datatotal$AMP))/sd(datatotal$AMP)
datatotal$NTR<-(datatotal$NTR-mean(datatotal$NTR))/sd(datatotal$NTR)
datatotal$I.NDVI<-(datatotal$I.NDVI-mean(datatotal$I.NDVI))/sd(datatotal$I.NDVI)


#####Calculating indices of multifunctionality (M5: 5 functions)
colnames(datatotal)
M5<-rowMeans(datatotal[,c(20,21,22,23,24)])
datatotal<-cbind(datatotal,M5)


#####Log-transfromation of multifunctionality
logM5<-log(datatotal$M5-min(datatotal$M5)+1)
datatotal<-cbind(datatotal,logM5)

   加载 MuMIn这个包做模型选择

代码是

library(MuMIn)
mod12<-lm(logM5 ~ LAT + SINLONG + COSLONG +   
            ARIDITY + SLO + SAND + PH + I(PH^2) + ELEVATION+
            CWM_logSLA + I(CWM_logSLA^2)+ CWV_logSLA + I(CWV_logSLA^2) +  CWS_logSLA + CWK_logSLA + I(CWK_logSLA^2) +
            CWM_logH + I(CWM_logH^2)+ CWV_logH + I(CWV_logH^2) +  CWS_logH + CWK_logH + I(CWK_logH^2) +
            SR
          , data=datatotal)
# 这一步要好长时间
dd12<-dredge(mod12, subset = ~ LAT & SINLONG & COSLONG & ARIDITY & SLO & SAND & PH &SR & ELEVATION &   
               dc(CWM_logSLA,I(CWM_logSLA^2)) & dc(CWV_logSLA,I(CWV_logSLA^2)) & dc(CWK_logSLA,I(CWK_logSLA^2)) 
             & dc(CWM_logH,I(CWM_logH^2)) & dc(CWV_logH,I(CWV_logH^2)) & dc(CWK_logH,I(CWK_logH^2)), 
             options(na.action = "na.fail"))

subset(dd12,delta<2)
de12<-model.avg(dd12, subset = delta < 2)
summary(de12)
 

R语言是怎么做方差分解的R语言是怎么做方差分解的

这一步得到的数据就是论文中 的figure4a

R语言是怎么做方差分解的  
image.png

下期推文介绍如何利用得到的数据画图

这里遇到的问题是:

  • 1、 模型里有的变量会用     I()函数包起来,这个函数起到什么作用呢?
  • 2、模型选择那一步用到了     dc()函数,这个函数又起到什么作用呢?

“R语言是怎么做方差分解的”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


文章标题:R语言是怎么做方差分解的
网页链接:http://jkwzsj.com/article/gejcjj.html

其他资讯