Why does: (shape/scale) * (1:365/scale)^(shape - 1) return a vector of numbers but calling a function hasard(1:365,shape,scale) defined like: hazard <- function(x,shape,scale) { return (shape/scale) * (x/scale)^(shape - 1) } Only return a single value? It is like x becomes a single value passed as an argument. Thank you. Kevin
Try hazard <- function(x,shape,scale) { return ((shape/scale) * (x/scale)^(shape - 1)) } hazard(1:365,1,1) --jeff rkevinburton at charter.net wrote:> Why does: > > (shape/scale) * (1:365/scale)^(shape - 1) > > return a vector of numbers but calling a function > > hasard(1:365,shape,scale) > > defined like: > > hazard <- function(x,shape,scale) > { > return (shape/scale) * (x/scale)^(shape - 1) > } > > Only return a single value? It is like x becomes a single value passed as an argument. > > Thank you. > > Kevin > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
rkevinburton at charter.net wrote:> Why does: > > (shape/scale) * (1:365/scale)^(shape - 1) > > return a vector of numbers but calling a function > > hasard(1:365,shape,scale) > > defined like: > > hazard <- function(x,shape,scale) > { > return (shape/scale) * (x/scale)^(shape - 1) > } > > Only return a single value? It is like x becomes a single value passed as an argument. > >No, but "return" is syntactically like a function name. Watch your parentheses.... -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
2008/8/28 <rkevinburton at charter.net>:> hazard <- function(x,shape,scale) > { > return (shape/scale) * (x/scale)^(shape - 1) > } > > Only return a single value? It is like x becomes a single value passed as an argument. >It's evaluating return(shape/scale) and returning that! Add some extra parentheses: return( (shape/scale) * (x/scale)^(shape-1) ) seems to work. Are you a python programmer, perhaps? Barry
<rkevinburton <at> charter.net> writes:> > Why does: > > (shape/scale) * (1:365/scale)^(shape - 1) > > return a vector of numbers but calling a function > > hasard(1:365,shape,scale) > > defined like: > > hazard <- function(x,shape,scale) > { > return (shape/scale) * (x/scale)^(shape - 1) > } > > Only return a single value? It is like x becomes a single value passed as anargument.>I believe you have a couple of typos. Your function is returning shape/scale only. Try: hazard <- function(x,shape,scale) { return ((shape/scale) * (x/scale)^(shape - 1)) }
On Thu, 28 Aug 2008, rkevinburton at charter.net wrote:> Why does: > > (shape/scale) * (1:365/scale)^(shape - 1) > > return a vector of numbers but calling a function > > hasard(1:365,shape,scale) > > defined like: > > hazard <- function(x,shape,scale) > { > return (shape/scale) * (x/scale)^(shape - 1)^^^^^^^^^^^^^^^^^^^^ return() is a function, its argument is returned, everything afterwards is ignored. And shape/scalar is probably a scalar in your case... So either change to return((shape/scale) * (x/scale)^(shape - 1)) or simply (shape/scale) * (x/scale)^(shape - 1) without explicitely calling return().> } > > Only return a single value? It is like x becomes a single value passed as an argument. > > Thank you. > > Kevin > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of > rkevinburton at charter.net > Sent: Thursday, August 28, 2008 11:36 AM > To: r-help at r-project.org > Subject: [R] Function not returning a vector? > > Why does: > > (shape/scale) * (1:365/scale)^(shape - 1) > > return a vector of numbers but calling a function > > hasard(1:365,shape,scale) > > defined like: > > hazard <- function(x,shape,scale) > { > return (shape/scale) * (x/scale)^(shape - 1) > } > > Only return a single value? It is like x becomes a single > value passed as an argument. > > Thank you. > > Kevin >Change your function definition to one of the following (I don't know which is "better"). hazard <- function(x,shape,scale) { return( (shape/scale) * (x/scale)^(shape - 1) ) } hazard <- function(x,shape,scale) { (shape/scale) * (x/scale)^(shape - 1) } Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204