かなり大きなデータフレームでフィッティング関数を実行しようとしましたが、"big_group"
と'small_group'
という変数でグループ化されています。 特に、big_group
の内側にあるsmall_group
のすべての予測値とcoefs値を取得しようとしています。エラー:結果は位置のデータフレームではありません。
つまり、これらの新しい列をdo({
関数の最後にある新しいdata.frameに追加しようとしています。
このデータのグループの中には、データポイントが不足している、または「初期パラメータ推定値で単数の勾配行列」エラーのために装着できないものがあります。
だから、私はhow-do-i-ignore-errors-and-continue-processing-list-itemsのこのポストからtryCatch
方法を使用して、私は@Koshke
OTHの次の応答を使用し、この問題を解決した後、私はエラーに遭遇するようになっ
を言っていますError: Results are not data frames at positions: 3
このエラーについては、discussionsがありますが、私の問題に実装する方法を理解できませんでした。
これは私の再現可能な例です。 (この例は私の実際のデータと似ているので、私はこのような例を作りました)
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!
}})
})
これはデータのようです。 @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
感謝の男私は実際にそれをappriciate。私の実際のデータをさらにチェックしてみましょう。 – Alexander
@Alexander確かにnpの歓声 –
こんにちは私は私の実際のデータとこの再現可能な例についての問題を発見しました。期待しているのは 'group_by(big_group、small_group) 'なので、予測とフィッティングの係数は各小グループの内部で異なるはずです。私はこのコードはそれを提供していないと思う。 – Alexander