本文介绍了"lm.fit 0(非NA)情况下的错误"成功运行r脚本多次后出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个令人困惑的问题.我正在为大型数据集计算线性模型,并使用"geom_text_repel"将方程式粘贴到图形上. 我多次成功运行脚本,但突然开始出现以下错误 :

这太疯狂了,因为我什么都没做.我已经广泛阅读了有关此问题的信息,但尚未找到解决方案.许多人说这是由于数据集的每一行都有NA,因此缺少协变量(

解决方案

是的,您在ROA子集中没有出现某些深度级别,因此这些数据集根本没有观测值.

 >Holes_SO $ depth<-factor(holes_SO $ depth)>ro_aue_SO<-子集(holes_SO,holes_SO $ field =="ROA")>表(ro_aue_SO $深度)0 20 30 60 901204 0 4 3 3 0>ro_split<-分割(ro_aue_SO,ro_aue_SO $ depth)>sapply(ro_split,nrow)0 20 30 60 901204 0 4 3 3 0>ms<-lapply(ro_split,function(x)lm(co2_flux_mol_c_m2_s1〜soc_concentration_kg_m3-1,data = x))lm.fit(x,y,offset = offset,singular.ok = singular.ok,...)中的错误(来自#1):0(非NA)案例 

删除这些内容不会留下任何错误.

  ro_aue_SO<-子集(holes_SO,Holes_SO $ field =="ROA")ro_aue_SO<-下降级别(ro_aue_SO)ro_split<-分割(ro_aue_SO,ro_aue_SO $ depth)ms<-lapply(ro_split,function(x)lm(co2_flux_mol_c_m2_s1〜soc_concentration_kg_m3-1,data = x)) 

Here is a puzzler. I'm calculating a linear model for a large dataset and using "geom_text_repel" to paste the equation onto the graphs. I ran my script successfully many times, but suddenly started getting the following error:

This is maddening because I didn't change anything. I've read extensively about this problem, but have yet to find a solution. Many people say it's due to having NAs in each line of the dataset, and therefore missing covariates (lm called from inside dlply throws "0 (non-NA) cases" error [r] and R linear regression issue : lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) and Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) 0 non-na cases). My dataset is quite complete:

> apply(ro_aue_SO,1,function(x) sum(is.na(x)))
40 41 42 43 44 45 46 47 48 49 50 51 52 53
 0  0  0  0  0  0  0  0  0  0  0  0  0  1

There are other subsets of this data with ZERO NAs, and I still get the same error.

I've tried using na.action = na.omit, I used traceback() on the error to get more insight, I've tried different data input methods - nothing is working. Because the error message just appeared after the script running perfectly for several hours, I'm wondering if this is a system problem. I'm using RStudio v1.0.153 and r 3.4.2 on OSX 10.12.6.

Help! Help! Help! And thank you, in advance.

Here is my (simplified) code:

holes_SO <- read.csv(file = 'data.csv', sep = ",", header = TRUE)
holes_SO$depth <- factor(holes_SO$depth)
ro_aue_SO <- subset(holes_SO, holes_SO$field == "ROA")
ot_slope_SO <- subset(holes_SO, holes_SO$field == "OTS")

#Set-up the empty equation
lm_origin_eqn <- function(m){
  eq <- substitute(italic(y) == b %.% italic(x)*","~~italic(r)^2~"="~r2,
                   list(b = format(coef(m)[1], digits = 2),
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));
}

roa_RTO <- ggplot(data = ro_aue_SO, aes(x = soc_concentration_kg_m3, y = co2_flux_µmol_c_m2_s1, color = depth, shape = depth)) +
  geom_point(size = 3) +
  labs(x = "SOC concentration", y = "CO2 Flux") +
  labs(color="Depth", shape= "Depth") +
  ggtitle(expression('RO Aue, CO'[2]*'')) +
  geom_smooth(aes(color = depth), method=lm, se=FALSE, formula=y~x-1, fullrange = TRUE) +
  xlim(-5,45) +
  theme(plot.title = element_text(size = 16, hjust = 0.5, face = "bold"),
        axis.text = element_text(size = 10),
        axis.title = element_text(size = 12)) +
  scale_color_discrete(drop=FALSE) +
  scale_shape_discrete(drop=FALSE)

#THIS IS WHERE THE ERROR OCCURS
#fill in the linear equation
  roa_eqns <- ro_aue_SO %>% split(.$depth) %>%
  map(~ lm(co2_flux_µmol_c_m2_s1 ~ soc_concentration_kg_m3 - 1, data = .)) %>%
  map(lm_origin_eqn) %>%
  do.call(rbind, .) %>%
  as.data.frame() %>%
  set_names("equation") %>%
  mutate(depth = rownames(.))

#paste equations onto graph
roa_RTO_equations <- roa_RTO + geom_text_repel(data = roa_eqns, aes(x = c(0, 0, 0, 0), y = c(125, 115, 105, 95), label = equation),
                                               parse = TRUE, segment.size = 0, show.legend = FALSE)

And a small sample of the data (generated using "dput(holes_SO)"):

structure(list(sample_id = structure(c(1L, 2L, 3L, 4L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 29L, 30L, 31L, 32L, 27L, 28L, 33L, 36L, 37L, 38L, 39L,
34L, 35L, 5L, 6L, 7L, 8L, 9L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 50L, 51L, 52L, 53L), .Label = c("OTS1-0", "OTS1-30",
"OTS1-60", "OTS1-90", "OTS10-0", "OTS10-20", "OTS10-30", "OTS10-60",
"OTS10-90", "OTS2-0", "OTS3-0", "OTS3-30", "OTS3-60", "OTS3-90",
"OTS4-0", "OTS5-0", "OTS5-30", "OTS5-60", "OTS5-90", "OTS6-0",
"OTS7-0", "OTS7-20", "OTS7-30", "OTS7-60", "OTS7-90", "OTS8-0",
"OTS8-120A", "OTS8-120B", "OTS8-20", "OTS8-30", "OTS8-60", "OTS8-90",
"OTS9-0", "OTS9-120A", "OTS9-120B", "OTS9-20", "OTS9-30", "OTS9-60",
"OTS9-90", "ROA1-0", "ROA1-30", "ROA1-60", "ROA1-90", "ROA2-0",
"ROA2-30", "ROA3-0", "ROA3-30", "ROA3-60", "ROA3-90", "ROA4-0",
"ROA4-30", "ROA4-60", "ROA4-90"), class = "factor"), site = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("OT", "RO"), class = "factor"), field = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("OTS", "ROA"), class = "factor"),
    hole_number = c(1L, 1L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 4L, 5L,
    5L, 5L, 5L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
    8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
    1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L),
    depth = c(0L, 30L, 60L, 90L, 0L, 0L, 30L, 60L, 90L, 0L, 0L,
    30L, 60L, 90L, 0L, 0L, 20L, 30L, 60L, 90L, 0L, 20L, 30L,
    60L, 90L, 120L, 120L, 0L, 20L, 30L, 60L, 90L, 120L, 120L,
    0L, 20L, 30L, 60L, 90L, 0L, 30L, 60L, 90L, 0L, 30L, 0L, 30L,
    60L, 90L, 0L, 30L, 60L, 90L), co2_flux_µmol_c_m2_s1 = c(1.710293078,
    0.30924686, 0.36469938, 0.227477037, 1.254479063, 0.752737414,
    2.257215969, 11.50282226, 3.566654093, 0.69900321, 1.591361818,
    13.92149665, 22.73002129, 22.45049, 1.109443533, 7.406644295,
    7.855618003, 17.78010488, 6.471314337, 5.315970134, 6.347455312,
    11.54719043, 10.11479135, 11.47752926, 2.805488908, 5.222756475,
    4.377681384, 7.173613131, 14.51864231, 9.729229653, 4.564367185,
    10.17710718, 7.70956059, 4.382202183, 3.321182297, 3.858269154,
    7.542932281, 19.88469738, 10.55216436, 3.572542676, 6.530127468,
    10.78088543, 12.82422246, 3.093747739, 6.956941294, 3.316715055,
    8.781949843, 7.684561849, 6.142716566, 2.69743231, 9.67046938,
    7.018872033, 9.475929618), soc_concentration_kg_m3 = c(16.57,
    1.28, 1.86, 1.63, 16.88, 16.8, 6.59, 5.7, 1.33, 15, 15.67,
    3.8, 3.95, 3.95, 17.17, 20.5, 21.1, 4.94, 4.27, 2.43, 14.9,
    16.52, 4.12, 4.59, 4.59, 4.24, 4.24, 15.36, 15.93, 15.93,
    7.14, 7.14, 3.87, 3.87, 19.21, 20.24, 6.45, 5, 4.85, 40,
    7.78, 7.78, 3.6, 41.25, 23, 36.67, 23.04, 12.4, 3.33, 35.71,
    9.66, 12.31, NA)), .Names = c("sample_id", "site", "field",
"hole_number", "depth", "co2_flux_µmol_c_m2_s1", "soc_concentration_kg_m3"
), class = "data.frame", row.names = c(NA, -53L))

And here is what I DID get, and should STILL get (with slightly different colors/labels), from running the above script:

解决方案

Yeah, you've got some levels of depth that don't occur in the ROA subset, so those data sets have no observations at all.

> holes_SO$depth <- factor(holes_SO$depth)
> ro_aue_SO <- subset(holes_SO, holes_SO$field == "ROA")
> table(ro_aue_SO$depth)

  0  20  30  60  90 120
  4   0   4   3   3   0
> ro_split <- split(ro_aue_SO, ro_aue_SO$depth)
> sapply(ro_split, nrow)
  0  20  30  60  90 120
  4   0   4   3   3   0
> ms <- lapply(ro_split, function(x) lm(co2_flux_mol_c_m2_s1 ~ soc_concentration_kg_m3 - 1, data = x))
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) (from #1) :
  0 (non-NA) cases

Removing those leaves no errors.

ro_aue_SO <- subset(holes_SO, holes_SO$field == "ROA")
ro_aue_SO <- droplevels(ro_aue_SO)
ro_split <- split(ro_aue_SO, ro_aue_SO$depth)
ms <- lapply(ro_split, function(x) lm(co2_flux_mol_c_m2_s1 ~ soc_concentration_kg_m3 - 1, data = x))

这篇关于"lm.fit 0(非NA)情况下的错误"成功运行r脚本多次后出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:11