0
votes

I am a beginner on R, working on water quality data. PLease excuse my formatting mistakes. I am trying to run "nls" on my dataset. Running the script:

testingQModel<-nls(GR ~ GRm * (1-Kq/Q), data = testingQ, start = list(Kq = min(testingQ$Q), GRm = max(testingQ$GR)))

I get the following error:

Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf

The dataset does not have NAs and is all numeric. I ran range(testingQ, na.rm = TRUE) also with range(testingQ, na.rm = FALSE) just to give it a try either way and it returned maximum and minimum values in dataset all right. I am not sure what else to try. Look forward to a solution from someone! Thanks.

2
Try range(testingQ$Q) and range(testingQ$GR) to see if you still get a max and min that aren't Inf for your start= values. I suspect that is where the problem lies.thelatemail
Is testingQ a data frame?jlhoward
Yes, testingQ is the data frame consisting of two columns Q and GR. Ran the script range(testingQ$Q) and range(testingQ$GR), both return [1] Inf -Inf. I have no clue though why it would be like that. I assumed it would return max and min values in respective columns. I tried plugging in initial values for GRm and Kq too instead of testingQ$GR etc but it returned same warning messages. Those are the two parameters I am trying to calculate the values of. I would appreciate little more help!Learner
@Learner - something is screwy with those two variables. They are likely empty if you are getting a range of -Inf to Inf. Try just testingQ[,"Q"] and see what it returns - also check str(testingQ[,"Q"]) and length(testingQ[,"Q"]) to see what the structure of the object is.thelatemail
I ran all strings and each of them returned Error in [.data.frame(testingQ, , "Q") : undefined columns selected whereas a call for Q returns the column I defined as Q. Just for record, I defined Q as Q<-testingQ[,1] and GR<-testingQ[,2]. This is how I defined variables while working with different functions and it worked fine. Or maybe I was just lucky not to encounter this problem.Learner

2 Answers

1
votes

Summarizing what you already seem to have solved and written in the comments:

  • issue had nothing to do with nls or max
  • issue was due to a bad access to a dataframe column, either bad syntax or else you had shadowed the definitions of Q, GR by defining variables with those names
  • this then gave you an empty vector, which causes max to return Inf/-Inf
  • solution was to fix the column access, or don't define the offending shadowing variables

My tip: summary, max and min are a pain in R since they don't handle either NAs or completely empty inputs well. So always either eyeball their input vector to double-check for sanity, or else assign their input to a variable and inspect that.

0
votes

I agree with smci, summary function like min are painful in R.

There is a solution for this in the hablar package that solves that min/max returns Inf when given an empty vector. The function s converts an empty vector (NULL) to NA.

The problem

min(NULL)

[1] Inf
Warning message:
In min(NULL) : no non-missing arguments to min; returning Inf

Solution

library(hablar)

min(s(NULL))
[1] NA

disclaimer I am biased for this solution since I authored the package.