素食主义者r包中的rda测试错误。变量未正确读取

我正在尝试使用纯素食包执行简单的RDA,以使用以下数据框测试深度,流域和扇区对遗传种群结构的影响。

datafile

“ ALL”变量是遗传种群分配(结构)。

如果我的数据链接无法正常工作,我将在此处粘贴数据框的一个片段。

素食主义者r包中的rda测试错误。变量未正确读取

我以这种方式读取数据:

RDAmorph_Oct6 <- read.csv("RDAmorph_Oct6.csv")

我的问题有两个方面: 1)我似乎无法正确读取我的遗传变量。我尝试了三件事来解决此问题。

gen=rda(ALL ~ Depth + Basin + Sector,data=RDAmorph_Oct6,na.action="na.exclude")
Error in eval(specdata,environment(formula),enclos = globalenv()) : 
  object 'ALL' not found
In addition: There were 12 warnings (use warnings() to see them)

所以,我尝试了类似的事情:

> gen=rda("ALL ~ Depth + Basin + Sector",na.action="na.exclude")
Error in colMeans(x,na.rm = TRUE) : 'x' must be numeric

所以我指定了数字

> RDAmorph_Oct6$ALL = as.numeric(RDAmorph_Oct6$ALL)
> gen=rda("ALL ~ Depth + Basin + Sector",na.rm = TRUE) : 'x' must be numeric

我真的很困惑。我也尝试过用dataset$variable指定每个变量,但这也不起作用。

奇怪的是,如果我查看环境变量对不同的复合变量的影响,我可以得到一个rda。

MC = RDAmorph_Oct6[,5:6]
H_morph_var=rda(MC ~ Depth + Basin + Sector,na.action="na.exclude")

请注意,我确实尝试仅提取上述遗传rda的ALL列。这也不起作用。 无论如何,这导致了我的第二个问题。

当我尝试绘制rda时,会得到一个超级奇怪的图。注意三个地方的五个点。我不知道这些是哪里来的。

素食主义者r包中的rda测试错误。变量未正确读取

我将不得不绘制遗传rda的图,我想我会提出同样的问题,所以我想现在就问。

我看过几本教程,并尝试过对每个问题进行多次迭代。我认为这里是最好的总结。如果有人可以给我一些线索,我将不胜感激。

fkesn 回答:素食主义者r包中的rda测试错误。变量未正确读取

文档?rda说,用于指定模型的公式的左侧必须是数据矩阵。您无法在data对象中将变量的名称作为左侧传递(或至少在有预料的情况下将其传递),这样做会暴露我们解析公式的错误,这将导致进一步的错误)。

您想要的是一个数据框,其中包含用于公式左侧的变量ALL

这有效:

library('vegan')
df <- read.csv('~/Downloads/RDAmorph_Oct6.csv')

ALL <- df[,'ALL',drop = FALSE]

请注意,drop = FALSE可以阻止R删除空维度(即将单列数据帧转换为矢量。

然后您的原始通话有效:

ord <- rda(ALL ~ Basin + Depth + Sector,data = df,na.action = 'na.exclude')
,

问题在于rda期望公式的第一部分(代码中的ALL)具有单独的df,而没有使用data =参数中的df。

如上所述,您可以使用分析所需的变量创建一个新的df,但这是一个单行解决方案,也应该可以使用:

gen <- rda(RDAmorph_Oct6$ALL ~ Depth + Basin + Sector,data = RDAmorph_Oct6,na.action = na.exclude)

,

这在某种程度上类似于加文·辛普森的答案。数据框中的分类向量也存在问题。您可以使用library(data.table)rowid函数将分类变量设置为唯一的整数。最优选地,不使用它们。我也想将ID向量设置为站点名称,但是现在我太懒了。

library(data.table)
RDAmorph_Oct6 <- read.csv("C:/........../RDAmorph_Oct6.csv")

#remove NAs before. I like looking at my dataframes before I analyze them.
RDAmorph_Oct6 <- na.omit(RDAmorph_Oct6)

#I removed one duplicate
RDAmorph_Oct6 <- RDAmorph_Oct6[!duplicated(RDAmorph_Oct6$ID),]

#Create vector with only ALL
ALL  <- RDAmorph_Oct6$ALL

#Create data frame with only numeric vectors and remove ALL
dfn  <- RDAmorph_Oct6[,-c(1,4,11,12)]

#Select all categorical vectors.
dfc  <- RDAmorph_Oct6[,c(1,12)]

#Give the categorical vectors unique integers doesn't do this for ID (Why?).
dfc2 <- as.data.frame(apply(dfc,2,function(x) rowid(x)))

#Bind back with numeric data frame
dfnc <- cbind.data.frame(dfn,dfc2)

#Select only what you need
df   <- dfnc[c("Depth","Basin","Sector")]

#The rest you know
rda.out <- rda(ALL ~ .,data=df,scale=T)
plot(rda.out,scaling = 2,xlim=c(-3,2),ylim=c(-1,1))

#Also plot correlations
plot(cbind.data.frame(ALL,df))

扇形和深度变化最大。几乎合乎逻辑,因为仅使用了三个向量。将整数分配给分类向量可能根本没有任何意义。该函数将自上而下的唯一整数分配给以下唯一字符串。我也不太确定要回答哪个问题。基于此,您可以组织数据框。

本文链接:https://www.f2er.com/3084442.html

大家都在问