Getting error "'to' must be a finite number" Plotting a function for adstock

2.2k Views Asked by At

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

1

There are 1 best solutions below

6
On

Looking at your error message alone (without access to the data), it seems that you likely have NA or missing values in myData$Spend. If that's the case, then max(...) will return NA instead of the maximum. Take this example:

> x <- c(1:10, 30, 515, 2:18)
> max(x)

[1] 515

If we add an NA value into the vector, we'll get NA:

> y <- c(1:10, 30, 515, 2:18, NA)
> max(y)

[1] NA

If you can tell max() to ignore any missing values by setting na.rm = TRUE and it will work:

> max(y, na.rm=TRUE)

[1] 515

So, I'm guessing setting na.rm=TRUE should fix your problem in this line:

curveDFx = seq(from=0, to=max(myData$Spend, na.rm=TRUE)*2, length.out=10000)

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:

> df <- data.frame(x=1:10, y=1:10)
[1] 10
> max(df$Y)
[1] -Inf
Warning message:
In max(df$Y) : no non-missing arguments to max; returning -Inf

In this case, I'm guessing Spend is not included as a column named in myData. Perhaps a typo?