Please see my Rcode below with data
channelName = as.character(myData$TV_total[1])
maxX = 1.05*max(myData$TV_total)
maxY = 1.05*max(myData$Total)
myPlotDataDF = data.frame(Return = myData$Total, Spend = myData$TV_total)
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 = dollar) +
scale_y_continuous(labels = comma) +
ggtitle(paste(channelName))
simpleScatterPlot
girafe(ggobj = simpleScatterPlot)
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)
}
startValVec = c(25000,100,1.5,100000)
minValVec = c(0,0,1.01,1)
maxValVec = c(500000, 500000, 2, 100000)
optim.parms<-nlminb(objective=Ufun,start=startValVec,
lower=minValVec,
upper=maxValVec,
control=list(iter.max=100000,eval.max=2000),
Spend = myData$Spend,
Return = myData$Return)
optim.parms
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(myData$Spend)*2, length.out=10000)
curveDFy = b+(a-b)*((curveDFx^c)/(d+(curveDFx^c)))
curveDF = data.frame(Spend = curveDFx, Return = curveDFy)
Then the error its throwing
Error in seq.default(from = 1, to = max(myData$Spend) * 2, length.out = 10000) : 'to' must be a finite number In addition: Warning message: In max(myData$Spend) : no non-missing arguments to max; returning -Inf
Looking at your error message alone (without access to the data), it seems that you likely have
NA
or missing values inmyData$Spend
. If that's the case, thenmax(...)
will returnNA
instead of the maximum. Take this example:If we add an
NA
value into the vector, we'll getNA
:If you can tell
max()
to ignore any missing values by settingna.rm = TRUE
and it will work:So, I'm guessing setting
na.rm=TRUE
should fix your problem in this line:EDIT:
Just noticed that OP's error message is
Error inn max(myData$Spend) : no non-missing arguments to max; returning -Inf
. Returning-Inf
will happen when the vector you reference has a length of zero.. in other words, an empty vector. This means that the likely explanation here is that you are referencing a column that does not exist in the dataframe. It could be a misspelling or typo being the most likely souce of this error.Example:
In this case, I'm guessing
Spend
is not included as a column named inmyData
. Perhaps a typo?