rbass

How to store large outcomes R?


I am using the Bass diffusion model and by doing a for-loop I found the parameters m, p, and q of approximately 5000 products. It is a lot of work to put these parameters into an excel sheet manually. Does someone know an easy way to store those values in R? In a table for instance? This is my code:

PID=unique(TotalBass4$ProdID)

BassStored=NULL
k=0
pidlistNAs=NULL
for(pid in PID){
  k=k+1
  out <- lm(Sales ~ Cumsaleslag + Cumsalessqrt, data=subset(TotalBass4,subset=ProdID==pid))
  P1a <- out$coef[1]
  P1b <- out$coef[2]
  P1c <- out$coef[3]
  P1mplus <- (-P1b+sqrt(P1b**2-4*P1a*P1c))/(2*P1c)
  P1mminus <- (-P1b-sqrt(P1b**2-4*P1a*P1c))/(2*P1c)
  m <- P1mminus
  p <- 1/m
  q <- P1b+p
  cmsl=subset(TotalBass4,subset=ProdID==pid)$Cumsaleslag
  Spred <- Bassmodel(p, q, m, cmsl,T=30)$Sales
  Spred <- ts(Spred)
  BassStored[[k]]=list(parm=c(m,p,q),ProdID=pid) ## Spred=Spred
  names(BassStored[[k]]$parm)=c("m","p","q")
  if(is.na(P1c))
  pidlistNAs=c(pidlistNAs,pid)
  if((k%%10)==0)
  print(k)
  }

BassStored[1]


Solution

  • Some reproducible data for the example:

    set.seed(2017-12-20)
    
    p <- runif(10, min=0.006293, max=0.00689)
    m <- runif(10, min=67380.15, max=68980.95)
    q <- runif(10, min=0.61809, max=0.65804)
    
    lapply(1:10, function(i) {
      list(parm=c(m=m[i], p=p[i], q=q[i]), ProdID=i)
    }) -> BassStored
    

    You’ve already created something like this and likely still have it loaded in memory in R:

    str(BassStored)
    ## List of 10
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.79e+04 6.69e-03 6.47e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 1
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.81e+04 6.45e-03 6.29e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 2
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.86e+04 6.37e-03 6.18e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 3
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.78e+04 6.54e-03 6.55e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 4
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.88e+04 6.52e-03 6.45e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 5
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.79e+04 6.38e-03 6.19e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 6
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.75e+04 6.63e-03 6.21e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 7
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.76e+04 6.57e-03 6.24e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 8
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.79e+04 6.83e-03 6.55e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 9
    ##  $ :List of 2
    ##   ..$ parm  : Named num [1:3] 6.88e+04 6.61e-03 6.29e-01
    ##   .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
    ##   ..$ ProdID: int 10
    

    We can turn it into a data frame:

    do.call(
      rbind.data.frame,
      lapply(1:length(BassStored), function(i){
        as.list(unlist(BassStored[i]))
      })
    ) -> xdf
    
    xdf
    ##      parm.m      parm.p    parm.q ProdID
    ## 2  67860.06 0.006689309 0.6468014      1
    ## 21 68054.35 0.006451261 0.6286121      2
    ## 3  68640.19 0.006372309 0.6181186      3
    ## 4  67829.24 0.006541486 0.6551225      4
    ## 5  68807.85 0.006517481 0.6454875      5
    ## 6  67886.29 0.006382578 0.6194927      6
    ## 7  67542.34 0.006625390 0.6212089      7
    ## 8  67635.12 0.006566107 0.6239669      8
    ## 9  67878.34 0.006826642 0.6545225      9
    ## 10 68778.44 0.006609701 0.6287901     10
    

    Clean up the names:

    xdf <- setNames(xdf, c("m", "p", "q", "ProdID"))
    
    xdf
    ##           m           p         q ProdID
    ## 2  67860.06 0.006689309 0.6468014      1
    ## 21 68054.35 0.006451261 0.6286121      2
    ## 3  68640.19 0.006372309 0.6181186      3
    ## 4  67829.24 0.006541486 0.6551225      4
    ## 5  68807.85 0.006517481 0.6454875      5
    ## 6  67886.29 0.006382578 0.6194927      6
    ## 7  67542.34 0.006625390 0.6212089      7
    ## 8  67635.12 0.006566107 0.6239669      8
    ## 9  67878.34 0.006826642 0.6545225      9
    ## 10 68778.44 0.006609701 0.6287901     10
    

    And, write it out:

    write.csv(xdf, "bassmodel.csv")