首页 > 解决方案 > lm.fit 中的错误:“y”中的 NA/NaN/Inf。当尝试转换为对数刻度时

问题描述

我正在尝试通过使用 vegdist 在基础 R 中绘制线性回归模型,然后通过转换为对数刻度来拟合它们 我的数据框:

> str(un_sap.bray)
 'dist' num [1:72390] 0.53 0.228 0.48 0.124 0.648 ...
 - attr(*, "Size")= int 381
 - attr(*, "Labels")= chr [1:381] "als1" "als2" "als3" "als4" ...
 - attr(*, "Diag")= logi FALSE
 - attr(*, "Upper")= logi FALSE
 - attr(*, "method")= chr "bray"
 - attr(*, "call")= language vegdist(x = un_sap.otu, method = "bray")

我的代码:这些所有代码都在工作

> un_sap.bray <- vegdist(un_sap.otu,"bray")
> un_sap.xy.dist=dist(un_sap.xy[,c('x','y')])
> plot(log(un_sap.xy.dist),log(un_sap.bray))

> DDR.un_sap.lm=lm(log(un_sap.bray)~log(un_sap.xy.dist))
> summary(DDR.un_sap.lm)
> abline(DDR.un_sap.lm,col=2)

然后我尝试在 lm() 拟合散点图出现错误后计算 1-vegdist()

> un_sap.bray <- 1-vegdist(un_sap.otu,"bray")   #change to similarity
> un_sap.xy.dist=dist(un_sap.xy[,c('x','y')])
> plot(log(un_sap.xy.dist),log(un_sap.bray))

> DDR.un_sap.lm=lm(log(un_sap.bray)~log(un_sap.xy.dist))
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  NA/NaN/Inf in 'y'

我检查了数据中的 NA

> any(is.na(log(un_sap.bray)))
[1] FALSE
> any(is.na(log(un_sap.xy.dist)))
[1] FALSE

我怎样才能解决这个问题?有人能帮我吗...

标签: rplot

解决方案


推荐阅读