mrkb80
mrkb80

Reputation: 591

Understanding warning message

My code is as follows:

m<-c(9,17,33,65,129,257,513)
results<-matrix(,7,5)
results[,1]<-m

#methods
trap<-function(a,b,m,func)
{
  h=(b-a)/(m-1)
  x<-seq(a,b,h)
  y<-function(x) { 
    z<-eval(parse(text=func)) 
    return(z) 
  }
  result<-h*(0.5* y(x[1]) + sum(y(x[2:(length(x)-1)]))+ 0.5*y(x[length(x)]) )
  result
}

when I run the following command: trap(0,5,results[,1],"x^2") I get the intended output, but I also get a nasty warning message:

Warning messages:  
1: In if (n < 0L) stop("wrong sign in 'by'
argument") :   the condition has length > 1 and only the first element
will be used  
2: In if (n > .Machine$integer.max) stop("'by' argument
is much too small") :   the condition has length > 1 and only the
first element will be used  
3: In 0L:n : numerical expression has 7
elements: only the first used  
4: In (0L:n) * by :   longer object
length is not a multiple of shorter object length 
5: In if (by > 0)
pmin(x, to) else pmax(x, to) :   the condition has length > 1 and only
the first element will be used

So I set about trying to understand what is going, and it seems everything points to this: x<-seq(a,b,h) but my sequence by should never be negative, it should always create a length greater than 1 (I'm not sure what the other warning messages mean).

Can someone help me understand this message, so I can correct whatever I am being warned about?

Upvotes: 0

Views: 1188

Answers (2)

janos
janos

Reputation: 124824

Are you sure you are getting the intended output? Any way I read ?seq, it looks like the "by" parameter should be a number, not a vector as in your example. This is the reason you get the nasty warning, because h is not a single number but a vector.

And the sequence you get from seq(a,b,h) is very strange:

> seq(a,b,h)
[1] 0.00000000 0.31250000 0.31250000 0.23437500 0.15625000 0.09765625 0.05859375
[8] 4.37500000 2.50000000

I think seq is designed for generating monotonic sequences, but what comes out of your example is neither monotonic nor making any sense...

Are you really sure the final result is what you expected? In any case this looks like a misuse of seq, unless I'm missing something.

Upvotes: 1

IRTFM
IRTFM

Reputation: 263481

You can trigger errors so that traceback() will be available for debugging of warnings, athough that is mprobably not needed here;

 options(warn=2) # usual setting is 1

Type this to see what your function is seeing for the arguments:

> c(a=0, b=5,m= results[,1])
  a   b  m1  m2  m3  m4  m5  m6  m7 
  0   5   9  17  33  65 129 257 513

So that's where the first warning about seq( , , by=.) getting an excessively long argument comes from, (since h will be as long as the 'm' argument was. I think that mostly explains the other warnings as well. I do not know of a way to get the warn() mechanism to skip the first or up to the n-th waring but if you dropped into the browser you could do that:

?browser

You could also search SO for best practices for [r] debugging

Upvotes: 1

Related Questions