1

I have a dataset that I would like to fit a bi-linear model to. However, I would also like some justification for finding the transition point.

I wanted to loop through possible values for the transition point, separate the data using dplyr::filter(), and then fit separate linear models to the 2 new datasets.

Following this, I thought I could calculate the sum of the R squared values, for both linear models. I would then select the transition point with the highest sum of R squared values. I hoped this would give the transition point with the best average fit for the 2 linear models.

...however, I get an the following error when I run the below code:

error:

'Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases'

Code:

simpleTable<-data.frame(a=rep('group 1',10),
b=c(1,2,2.5,3,3.5,4.5,5,7,8,10),
c=c(8,15,18,19,22,24,25,29,31,35))

DF<- data.frame(k=1, Rsq.total=0); 
for (k in seq(from=0, to=10, by=0.1)){

  for (j in seq(from=1, to=length(simpleTable$a), by=1)){
    if(simpleTable$b[j]<=k){
      simpleTable$b1[j]<-simpleTable$b[j]; simpleTable$b2[j]<-0
    } else {simpleTable$b1[j]<-0; simpleTable$b2[j]<-simpleTable$b[j]}
  }

  simpleTable.1<-simpleTable %>% dplyr::filter(simpleTable$b1 != 0)
  simpleTable.2<-simpleTable %>% dplyr::filter(simpleTable$b2 != 0)

  LinMod.1<-lm(c ~ b1, data=simpleTable.1); Rsq.1<-summary(LinMod.1)$r.squared
  LinMod.2<-lm(c ~ b2, data=simpleTable.2); Rsq.2<-summary(LinMod.2)$r.squared

  Rsq.total<-Rsq.1+Rsq.2
  newRow<-data.frame(k, Rsq.total); DF<-rbind(DF, newRow)

}  

This example is with dummy data, but I think it describes my problem. Any help is appreciated.

d73mwf
  • 11
  • 1
  • 4
  • The error comes from one of the filtered data in the loop which is empty, So you may need to fix with `tryCatch` or `possibly` – akrun Jun 21 '18 at 14:53
  • The first time through the loop you filter out all the items with `simpleTable$b2 != 0` but since all b2 _are_ zero, you get an empty dataframe, hence an error with lm. – IRTFM Jun 21 '18 at 15:04

0 Answers0