0

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
.. ..        ...            ...             ...             ...           ...
Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Alexander
  • 4,527
  • 5
  • 51
  • 98
  • 1
    Looks like you've done lots of work and changes here... In your function you have "J" as a parameter to pass in, but in your code inside the function you have J1 and J2, which don't belong in your dataset. Then in your nlsML model you specify start for J and J2. Is that J1 instead of J? If you check all other things then it should work, as the "data=." is everything you need to create a model for each one of your groups. – AntoniosK Aug 20 '15 at 00:03
  • @AntoniosK. Oh at last somebody answered. Ok I fixed that initial starting parameters J1 and J2. Since this is a reproducible example not the real data I dont need to care about fitting parameters. So I can stop after 10 iterations with using `nls.lm.control`. But I cannot store `df_new` for further process because ERROR is coming out which says – Alexander Aug 20 '15 at 00:25
  • `Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates In addition: Warning message: In nls.lm(par = start, fn = FCT, jac = jac, control = control, lower = lower, : lmdif: info = -1. Number of iterations has reached `maxiter' == 10.` – Alexander Aug 20 '15 at 00:25
  • That's what I found as well. This is a model issue now. Something is wrong with the initial parameter estimates. Can you experiment with other numbers? I'll try to check it again when I have time. – AntoniosK Aug 20 '15 at 00:33
  • @AntoniosK. Ok I will check with the other parameters. By the way I tried above code with my real data for only one column `DQ0` vs `V` and I put the initial parameters as it should be. however, it seems to be `del1` and `del2` is not iterating same as here. I wonder about the `ifelse` part that I defined may be not correct. – Alexander Aug 20 '15 at 00:55

0 Answers0