# 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)

maxY = 1.5*max(db_bbw_new\$y) #myData\$Retur?

Return <- db_bbw_new\$y

?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 + (x - x)*((Spend^x)/(x + (Spend^x)))
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),
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
b = optim.parms\$par
c = optim.parms\$par
d = optim.parms\$par

curveDFy = b+(a-b)*((curveDFx^c)/(d+(curveDFx^c)))
curveDF = data.frame(Spend = curveDFx, Return=curveDFy)

maxY = 1.5*max(curveDFy, max(db_bbw_new\$y))

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.