How to use nlminb() function to model fit and fit it well?

I was trying to build Hill customized function, and wasn't successful with that. COuld you help me to find out what is wrong with the following:

     
     str(db_bbw_new)
     
     channelName = as.character(db_bbw_new$adcost[1])
     maxX = 1.5*max(db_bbw_new$adcost) # myData$Spend
     maxY = 1.5*max(db_bbw_new$y) #myData$Retur?
     
     Return <- db_bbw_new$y
     Spend <- db_bbw_new$adcost
     myPlotDataDF = data.frame(Return = db_bbw_new$y, Spend = db_bbw_new$adcost)
     
     ?ggplot()
     
     simpleScatterPlot <- ggplot(myPlotDataDF, aes(x = Spend, y = Return)) +
       geom_point(color="black") +
       theme(panel.background = element_rect(fill = 'grey85'),
             panel.grid.major = element_line(colour = "white")) +
       coord_cartesian(ylim = c(0,maxY), xlim = c(0,maxX)) +
       scale_x_continuous(labels = scales::dollar) +
       scale_y_continuous(labels = scales::comma) + 
       ggtitle(paste(channelName))
     
     simpleScatterPlot

 #We need a model that exhibits diminishing marginal returns.  Diminishing marginal return means that the rate of return will decrease the more you spend. The ADBUDG model is a very flexible model that incorporates diminishing marginal returns.  The ADBUDG model is defined as follows:
     
     
     Ufun<-function(x, Spend, Return) {
       predictedReturn = x[2] + (x[1] - x[2])*((Spend^x[3])/(x[4] + (Spend^x[3])))
       errorSq = (predictedReturn - Return)^2
       sumSqError = sum(errorSq)
       return(sumSqError)
     }
     

#Let's create some start values, as well as upper and lower bounds:

 startValVec = c(2000000,10,1.5, 90000000)  
     minValVec = c(0,0, 1.01 ,0)
     maxValVec = c(400000000, 400000000, 2, 600000000)  


optim.parms<-nlminb(objective=Ufun,start=startValVec,
                         lower=minValVec,
                         upper=maxValVec,
                         control=list(iter.max=1000000,eval.max=20000),
                         Spend = db_bbw_new$adcost,
                         Return = db_bbw_new$y)
     
     
     optim.parms
     


#Now that the plot will include the ADBUDG model, building the data frame to feed into ggplot2 is a little more complex. 
     a = optim.parms$par[1]
     b = optim.parms$par[2]
     c = optim.parms$par[3]
     d = optim.parms$par[4]
     
     curveDFx = seq(from=0, to=max(db_bbw_new$adcost)*2, length.out=10000)
     curveDFy = b+(a-b)*((curveDFx^c)/(d+(curveDFx^c)))
     curveDF = data.frame(Spend = curveDFx, Return=curveDFy)
     
     maxX = 1.5*max(curveDFx, max(db_bbw_new$adcost))
     maxY = 1.5*max(curveDFy, max(db_bbw_new$y))
     
     
     
     myPlotDataDF = data.frame(Return = db_bbw_new$y, Spend = db_bbw_new$adcost)
     optimLineDF = data.frame(Spend = curveDFx, Return = curveDFy)
     
     scatterPlotPlusFit <- ggplot(myPlotDataDF, aes(x = Spend, y = Return)) +
       geom_point(color="black", shape = 16) +
       theme(panel.background = element_rect(fill = 'grey85'),
             panel.grid.major = element_line(colour = "white")) +
       geom_line(data = optimLineDF, aes(x = Spend, y = Return, color = "darkgreen"))  +
       scale_color_manual(labels = "Optimized ADBUDG Fit",values=c('darkgreen')) +
       theme(legend.title=element_blank(), legend.position = "bottom") +
       coord_cartesian(ylim = c(0,maxY), xlim = c(0,maxX)) +
       scale_x_continuous(labels = scales::dollar) +
       scale_y_continuous(labels = scales::comma) + 
       ggtitle(paste(channelName, "Data & Model Fit", sep = " "))
     
     scatterPlotPlusFit

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.