0

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

Tushar B
  • 35
  • 8

1 Answers1

1

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?

chemdork123
  • 12,369
  • 2
  • 16
  • 32
  • Thanks for the response. I'll give it a shot. However, if you wanna take a dig with the data. Its there now. Cheers! – Tushar B Apr 14 '21 at 15:54
  • Most people won't click a link to grab data (including me) as a general precaution. Try running `max(myData$Spend)` first and see if you actually get a number value or `NA`. The key is definitely with that based on your error message. – chemdork123 Apr 14 '21 at 15:58
  • Understood! Its that I coudnt find a way to attach the data there. If you can direct me towards the right way I can attach it to the main code. Apologies! – Tushar B Apr 14 '21 at 16:00
  • so I tried running just the 'max(myData$Spend)' and its returning ' -Inf ' and a warning message : In max(myData$Spend) : no non-missing arguments to max; returning -Inf – Tushar B Apr 14 '21 at 16:05
  • 1
    This error is resulting from `max()` being applied to a vector that contains nothing (empty vector). You're probably referencing a column that does not exist in `myData`. Are you sure you have the spelling right? is it `myData$spend` instead of `myData$Spend`? If you type `myData$Spend`, do you get nothing or do you have a list of numbers? – chemdork123 Apr 14 '21 at 16:42
  • Brilliant! It worked. The data was not pulling in that vector correctly and then there was a typo. Thanks alot! – Tushar B Apr 14 '21 at 17:42