Hi R users, I try to build a function to compute odds ratio and relative risk however something wrong. I stuck for many hours but I really don't know how to solve it. Would someone please give me a hint?> OR.RR<-function(x){+ x <- as.matrix(any(dim(x)==2)) + OR<-(x[1,1]*x[2,2])/(x[1,2]*x[2,1]) + RR<-(x[1,1]/(sum(x[1,])))/(x[2,1]/(sum(x[2,]))) + return(OR);return(RR) + }> > tt<-matrix(data=1:4,nrow=2,ncol=2) > OR.RR(tt)Error in OR.RR(tt) : subscript out of bounds Many Thanks Tu -- View this message in context: http://www.nabble.com/Something-wrong-with-my-function-Please-Help-tp25656420p25656420.html Sent from the R help mailing list archive at Nabble.com.
On Tue, Sep 29, 2009 at 4:29 AM, Chunhao Tu <tu_chunhao at yahoo.com> wrote:> > Hi R users, > I try to build a function to compute odds ratio and relative risk however > something wrong. I stuck for many hours but I really don't know how to solve > it. Would someone please give me a hint? > >> OR.RR<-function(x){What is this line doing: ? ? ? x <- as.matrix(any(dim(x)==2)) If you run it on your 2x2 matrix you get this: > as.matrix(any(dim(tt)==2)) [,1] [1,] TRUE and then the rest of your code is working with that value of 'x', and so failing. I don't know what that line is there for - what do you think it is doing? You shouldn't get stuck for hours over something this simple. Try each line on its own and check the return value is what you think it should be. You can either type each line in to the > prompt, or learn to use the 'debug' function. See help(debug) for more. Barry
Chunhao Tu wrote:> Hi R users, > I try to build a function to compute odds ratio and relative risk however > something wrong. I stuck for many hours but I really don't know how to solve > it. Would someone please give me a hint? > > >> OR.RR<-function(x){ >> > + x <- as.matrix(any(dim(x)==2)) > + OR<-(x[1,1]*x[2,2])/(x[1,2]*x[2,1]) > + RR<-(x[1,1]/(sum(x[1,])))/(x[2,1]/(sum(x[2,]))) > + return(OR);return(RR) > + } > >> tt<-matrix(data=1:4,nrow=2,ncol=2) >> OR.RR(tt) >> > Error in OR.RR(tt) : subscript out of bounds > > Many Thanks > Tu >In addition to Barry Rowlingson: You can insert the browser() command at any point in the code to drop into that environment. In you case you can use: x = function(arg1, arg2) { browser() do_stuff(arg1, arg2) } Running this will drop you into the environment of function x, allowing you to run the function line by line, and allowing you to inspect the content of all objects. An alternative to browser() is to use the following command: options(error=recover) When a program generates an error, you drop into that environment, allowing you to better diagnose the problem. Also look at the traceback() command, this show you which chain of commands led to the error. cheers and good luck, Paul -- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +3130 274 3113 Mon-Tue Phone: +3130 253 5773 Wed-Fri http://intamap.geo.uu.nl/~paul
Did you run debug over your function? Load the library debug, and then run mtrace over your function. library(debug) ? mtrace hth On Tuesday 29 September 2009 04:29:37 Chunhao Tu wrote:> Hi R users, > I try to build a function to compute odds ratio and relative risk however > something wrong. I stuck for many hours but I really don't know how to > solve it. Would someone please give me a hint? > > > OR.RR<-function(x){ > > + x <- as.matrix(any(dim(x)==2)) > + OR<-(x[1,1]*x[2,2])/(x[1,2]*x[2,1]) > + RR<-(x[1,1]/(sum(x[1,])))/(x[2,1]/(sum(x[2,]))) > + return(OR);return(RR) > + } > > > tt<-matrix(data=1:4,nrow=2,ncol=2) > > OR.RR(tt) > > Error in OR.RR(tt) : subscript out of bounds > > Many Thanks > Tu-- Corrado Topi Global Climate Change & Biodiversity Indicators Area 18,Department of Biology University of York, York, YO10 5YW, UK Phone: + 44 (0) 1904 328645, E-mail: ct529 at york.ac.uk
On 09/29/2009 01:29 PM, Chunhao Tu wrote:> Hi R users, > I try to build a function to compute odds ratio and relative risk however > something wrong. I stuck for many hours but I really don't know how to solve > it. Would someone please give me a hint? > > >> OR.RR<-function(x){ >> > + x<- as.matrix(any(dim(x)==2)) > + OR<-(x[1,1]*x[2,2])/(x[1,2]*x[2,1]) > + RR<-(x[1,1]/(sum(x[1,])))/(x[2,1]/(sum(x[2,]))) > + return(OR);return(RR) > + } > >> tt<-matrix(data=1:4,nrow=2,ncol=2) >> OR.RR(tt) >> > Error in OR.RR(tt) : subscript out of bounds >Hi Tu, The "any" function only returns a single logical value. Thus you have at best a one element matrix. Your subscripts assume at least a four element matrix. Maybe what you are trying to do is this: OR.RR<-function(x) { if(any(dim(x)>2) || length(dim(x) > 2) stop("OR.RR only works with a 2x2 matrix") ... Jim