Conditional nls fitting with dplyr+broom

我们两清 提交于 2019-12-08 09:01:37

问题


I am using the dplyr and broom combination and try to fitting regression models depending on the condition inside of the data groups. Finally I want to extract the regression coefficients by each group.

So far I'm getting the same fitting results for all groups (Each group is separated with letters a:f) . It's the main problem.

library(dplyr)
library(minpack.lm)
library(broom)

direc <- rep(rep(c("North","South"),each=20),times=6)
V <- rep(c(seq(2,40,length.out=20),seq(-2,-40,length.out=20)),times=1)
DQ0 = c(replicate(2, sort(runif(20,0.001,1))))
DQ1 = c(replicate(2, sort(runif(20,0.001,1))))
DQ2 = c(replicate(2, sort(runif(20,0.001,1))))
DQ3 = c(replicate(2, sort(runif(20,0.001,1))))
No  =  c(replicate(1,rep(letters[1:6],each=40)))

df <-   data.frame(direc,V,DQ0,DQ1,DQ2,DQ3,No)

fit conditions can be described as follows; direc=North and if V<J1 do fitting with the equation exp((-t_pw)/f0*exp(-del1*(1-V/J1)^2)) else if direc=Southand V>J2 do fitting with the same equation. In both case, if V<J1& V>J2 are not satisfied return 1 for each case.

UPDATE I found that conditional nls can be possible conditional-formula-for-nls with the suggestion in this link.

nls_fit=nlsLM(DQ0~ifelse(df$direc=="North"&V<J1, exp((-t_pw)/f0*exp(-del1*(1-V/J1)^2)),1)*ifelse(df$direc=="South"&V>J2, exp((-t_pw)/f0*exp(-del2*(1-V/J2)^2)),1)
            ,data=df,start=c(del1=1,J1=15,del2=1,J2=-15),trace=T) 

nls_fit

Nonlinear regression model
  model: DQ0 ~ ifelse(df$direc == "North" & V < J1, exp((-t_pw)/f0 * exp(-del1 *     (1 - V/J1)^2)), 1) * ifelse(df$direc == "South" & V > J2,     exp((-t_pw)/f0 * exp(-del2 * (1 - V/J2)^2)), 1)
   data: df
   del1      J1    del2      J2 
  1.133  23.541   1.079 -20.528 
 residual sum-of-squares: 16.93

Number of iterations to convergence: 4 
Achieved convergence tolerance: 1.49e-08

On the other hand when I try to fit other columns such as DQ1,DQ2 and DQ3;

I tried nls_fit=nlsLM(df[,3:6]~ifelse(.....

Error in nls.lm(par = start, fn = FCT, jac = jac, control = control, lower = lower, : evaluation of fn function returns non-sensible value!

now the problem came down to multiple column fitting. How can I fit multiple columns DQ0:DQ3 ? I checked how to succinctly write a formula with many variables from a data frame? but couldn't find the solution to use in my data frame.


In addition when I do fitting for DQ0 column inside of its groups as you can see from the output same Del and J parameters are produced for all groups a:f

df_new<- df%>%
  group_by(No)%>%
  do(data.frame(model=tidy()))>%
  ungroup

df_new

Source: local data frame [24 x 6]

   No model.term model.estimate model.std.error model.statistic model.p.value
1   a       del1       1.132546        9024.255    1.255002e-04     0.9999000
2   a         J1      23.540764      984311.373    2.391597e-05     0.9999809
3   a       del2       1.079182       27177.895    3.970809e-05     0.9999684
4   a         J2     -20.527520     2362268.839   -8.689748e-06     0.9999931
5   b       del1       1.132546        9024.255    1.255002e-04     0.9999000
6   b         J1      23.540764      984311.373    2.391597e-05     0.9999809
7   b       del2       1.079182       27177.895    3.970809e-05     0.9999684
8   b         J2     -20.527520     2362268.839   -8.689748e-06     0.9999931
9   c       del1       1.132546        9024.255    1.255002e-04     0.9999000
10  c         J1      23.540764      984311.373    2.391597e-05     0.9999809
.. ..        ...            ...             ...             ...           ...

来源:https://stackoverflow.com/questions/32107224/conditional-nls-fitting-with-dplyrbroom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!