本文介绍了错误:结果不是以下位置的数据框:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个相当大的数据帧上运行一个拟合函数,该函数由名为 big_group 'small_group'
特别是,我试图获取 big_group 中每个 small_group 的预测和coefs值。



也就是说,我正在尝试在 do({函数。



由于缺少数据点或初始参数估计时的奇异梯度矩阵错误,无法拟合此数据的某些组。 / p>

因此,我使用了tryCatch 方法问题/ 4137736 /如何忽略错误并继续处理列表项?noredirect = 1& lq = 1>如何忽略错误并继续处理列表-项,我使用了@Koshke

的以下答案



OTH,解决此问题后,我遇到了错误

有些,但我不知道它如何实现我的问题。



这是我可复制的示例; (此示例与我的真实数据类似,这就是为什么我要构建这样的示例)

  library(minpack.lm)
库(dplyr)


set.seed(100)

data.list<-lapply(1:2,function(big_group){
xx<-c(sort(runif(5,1,5)),sort(runif(5,-8,-2)),rep(5,2))## I意向都加了最后两个5获得不合适的组

yy<-sort(runif(12,0,10))

small_group<-rep(c('a','b' ,'c'),times = c(5,5,2))## big_group

df<-data.frame(xx,yy,small_group,big_group)下的小群

df<-df%>%
group_by(big_group,small_group)%>%

do({
#fitting part
fit<-tryCatch(nlsLM(yy〜k * xx / 2 + U,start = c(k = 1,U = 5),data =。,trace = T,
control = nls.lm .control(maxiter = 100)),error = function(e)NULL)

if(!( NULL%in%class(fit))){

new.range<-data.frame(xx = seq(1,10,length.out = nrow(。)))
预测<-预测(fit,newdata = new.range)
coefs<-data.frame(k = coef(fit)[1],U = coef(fit)[2])

data.frame(。,new.range,predicted,coefs,row.names = NULL)##这是我猜错的部分!

}})
})

这就是数据看起来像; @RomanLuštrik

  data.list<-lapply(1:2,function(big_group){
xx< -c(sort(runif(5,1,5)),sort(runif(5,-8,-2)),rep(5,2))##我故意将最后两个5加起来得到不合适的组
yy<-sort(runif(12,0,10))
small_group<-rep(c('a','b','c'),times = c(5,5, 2))## big_group
df以下的小组})


df <-bind_rows(data.list)
> df
xx yy small_group big_group
1 1.685681 1.302889 a 1
2 2.680406 1.804072 a 1
3 3.153395 3.306605 a 1
4 3.995889 3.486920 a 1
5 4.081206 6.293909 a 1
6 -6.333657 6.952741 b 1
7 -5.070164 7.775844 b 1
8 -4.705420 8.273034 b 1
9 -2.708278 8.651205 b 1
10- 2.428970 8.894535 b 1
11 5.000000 9.541577 c 1
12 5.000000 9.895641 c 1
13 1.830856 1.234872 a 2
14 2.964927 2.114086 a 2
15 3.413297 2.299059 a 2
16 4.121434 2.533907 a 2
17 4.536908 3.577738 a 2
18 -6.807926 4.451480 b 2
19 -6.585834 4.637012 b 2
20 -6.350680 5.913211 b 2
21 -6.157485 5.975753 b 2
22 -6.016821 6.471012 b 2
23 5.000000 6.763982 c 2
24 5.000000 9.605731 c 2


解决方案

这个怎么样?麻烦似乎是迫使传统的R代码与%&%;%管道一起使用,因此我只是在解决它。

 #库和选项---------------------- ----------------------------- 
库(minpack.lm)
库(dplyr)
set.seed(100)

#创建数据------------------------------- --------------------------
data.list<-lapply(1:2,function(big_group){
xx<-c(sort(runif(5,1,5)),sort(runif(5,-8,-2)),rep(5,2))## I都将最后两个5加到获取不合适的组

yy
small_group<-rep(c('a','b',' c'),times = c(5,5,2))## big_group

df中的小组})

df<-bind_rows(data.list)



#拟合模型--------- --------------------------------------------------
打印(我的理解是,您要为大型组和小型组的每种组合使用单独的模型拟合)

#创建适合水平的组
df $ big_small <-paste0(df $ big_grou p,df $ small_group)

#创建结果对象
df1<-structure(list(xx = numeric(0),yy = numeric(0),small_group = structure(integer( 0)、. Label = c( a,
b, c),class = factor),big_group =整数(0),big_small =字符(0),
xx.1 =数字(0),预测=数字(0),k =数字(0),
U =数字(0)),.名称= c( xx, yy, small_group , big_group,
big_small, xx.1, predicted, k, U),row.names = integer(0),class = data.frame)

#拟合模型,得到结果
for(b_s in unique(df $ big_small)){
fit<-tryCatch(nlsLM(yy〜k * xx / 2 + U,start = c(k = 1,U = 5),data = df [df $ big_small == b_s,],trace = T,
control = nls.lm.control(maxiter = 100)), error = function(e)NULL)

if(!( NULL%in%class(fit))){

new.range<-data.frame( xx = seq(1,10,length.out = nrow(df [df $ bi g_small == b_s,])))
预测<-预测(fit,newdata = new.range)
coefs<-data.frame(k = coef(fit)[1],U = coef(fit)[2])

df1 }
}





  df1 




I am trying to run a fitting function on a rather large data frame, grouped by a variable named "big_group" and 'small_group'.In particular, I am trying to get predictions and coefs values of every small_group inside of big_group.

That is, I'm trying to add these new columns to my new data.frame at the end of do({ function.

Some of the groups of this data cannot be fitted due to either lack of data points or "singular gradient matrix at initial parameter estimates" error.

So, I used tryCatch method from this post of how-do-i-ignore-errors-and-continue-processing-list-items and I used following answer of @Koshke

R : catching errors in `nls`

OTH, after solving this issue I come to encounter an error is saying

There is some discussions about this error but I could not figure it how to implement to my problem.

Here is my reproducible example; (This example is similar to my real data that's why I built the example like this)

library(minpack.lm)
library(dplyr)


set.seed(100)

data.list <- lapply(1:2, function(big_group) {
  xx <- c(sort(runif(5,1,5)),sort(runif(5,-8,-2)), rep(5,2))  ##I intentionall added the last two 5 to get unfitted groups

  yy<- sort(runif(12,0,10))

  small_group <- rep(c('a','b','c'),times=c(5,5,2)) ##small groups in under the big_group

  df <- data.frame(xx,yy,small_group,big_group)

  df <- df%>%
    group_by(big_group,small_group)%>%

  do({
  #fitting part
    fit <- tryCatch(nlsLM(yy~k*xx/2+U, start=c(k=1,U=5), data = ., trace=T,
                          control = nls.lm.control(maxiter=100)),error=function(e) NULL)

      if(!("NULL" %in% class(fit))){

    new.range<- data.frame(xx=seq(1,10,length.out=nrow(.)))
    predicted <- predict(fit, newdata =new.range)
    coefs <- data.frame(k=coef(fit)[1],U=coef(fit)[2])

    data.frame(., new.range,predicted,coefs,row.names=NULL) ##This is the part the error came from I guess!

}})
})

This is what the data looks like; @RomanLuštrik

data.list <- lapply(1:2, function(big_group) {
  xx <- c(sort(runif(5,1,5)),sort(runif(5,-8,-2)), rep(5,2))  ##I intentionall added the last two 5 to get unfitted groups
  yy<- sort(runif(12,0,10))
  small_group <- rep(c('a','b','c'),times=c(5,5,2)) ##small groups in under the big_group
  df <- data.frame(xx,yy,small_group,big_group)
})


df <- bind_rows(data.list)
 > df
          xx       yy small_group big_group
1   1.685681 1.302889           a         1
2   2.680406 1.804072           a         1
3   3.153395 3.306605           a         1
4   3.995889 3.486920           a         1
5   4.081206 6.293909           a         1
6  -6.333657 6.952741           b         1
7  -5.070164 7.775844           b         1
8  -4.705420 8.273034           b         1
9  -2.708278 8.651205           b         1
10 -2.428970 8.894535           b         1
11  5.000000 9.541577           c         1
12  5.000000 9.895641           c         1
13  1.830856 1.234872           a         2
14  2.964927 2.114086           a         2
15  3.413297 2.299059           a         2
16  4.121434 2.533907           a         2
17  4.536908 3.577738           a         2
18 -6.807926 4.451480           b         2
19 -6.585834 4.637012           b         2
20 -6.350680 5.913211           b         2
21 -6.157485 5.975753           b         2
22 -6.016821 6.471012           b         2
23  5.000000 6.763982           c         2
24  5.000000 9.605731           c         2
解决方案

How about this? The trouble seemed to be forcing the traditional R code to work with the %>% pipe, so I just worked around it.

# Libraries and Options ---------------------------------------------------
library(minpack.lm)
library(dplyr)
set.seed(100)

# Create the data ---------------------------------------------------------
data.list <- lapply(1:2, function(big_group) {
  xx <- c(sort(runif(5,1,5)),sort(runif(5,-8,-2)), rep(5,2))  ##I intentionall added the last two 5 to get unfitted groups

  yy<- sort(runif(12,0,10))

  small_group <- rep(c('a','b','c'),times=c(5,5,2)) ##small groups in under the big_group

  df <- data.frame(xx,yy,small_group,big_group)
})

df <- bind_rows(data.list)



# Fit the Model -----------------------------------------------------------
print("My understanding here is that you want a separate model fit for each combination of big group and small group")

# Create fit-level groups
df$big_small <- paste0(df$big_group, df$small_group)

# Create results object
df1 <- structure(list(xx = numeric(0), yy = numeric(0), small_group = structure(integer(0), .Label = c("a",
                      "b", "c"), class = "factor"), big_group = integer(0), big_small = character(0),
                      xx.1 = numeric(0), predicted = numeric(0), k = numeric(0),
                      U = numeric(0)), .Names = c("xx", "yy", "small_group", "big_group",
                                                  "big_small", "xx.1", "predicted", "k", "U"), row.names = integer(0), class = "data.frame")

# Fit model, get results
for(b_s in unique(df$big_small)){
  fit <- tryCatch(nlsLM(yy~k*xx/2+U, start=c(k=1,U=5), data = df[df$big_small==b_s,], trace=T,
                        control = nls.lm.control(maxiter=100)),error=function(e) NULL)

  if(!("NULL" %in% class(fit))){

    new.range<- data.frame(xx=seq(1,10,length.out=nrow(df[df$big_small==b_s,])))
    predicted <- predict(fit, newdata =new.range)
    coefs <- data.frame(k=coef(fit)[1],U=coef(fit)[2])

    df1 <- rbind(df1, data.frame(df[df$big_small==b_s,], new.range,predicted,coefs,row.names=NULL))
  }
}
df1

这篇关于错误:结果不是以下位置的数据框:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:05