Dear all I am trying to calculate the value of n using uniroot. Here is the message I am having: <<< Error in uniroot(integ, lower = 0, upper = 1000, n) : 'interval' must be a vector of length 2 >>> Please would you be able to give me an indication on why I am having this error message. Many thanks EXAMPLE BELOW: ## t = statistics test from t -distribution [-Inf,Inf] ## df == degree of freedom ###p <- p value diff <- 0.5 y <- function(t,n){ df <- 2*n-2 ncp1 <- sqrt((diff^2*n)/2) p <- 1- pt(t,df=df1) test <- qt((1-p),df=df1,ncp=ncp1)*(1/sqrt(2)) return(test) } integ <- function(t,n){ 1-integrate(y,lower=0,upper=3.6,n)$value -0.5 } uniroot(integ,lower=0,upper=1000) -- View this message in context: http://r.789695.n4.nabble.com/Uniroot-error-message-with-in-intergration-tp4634247.html Sent from the R help mailing list archive at Nabble.com.
On Fri, Jun 22, 2012 at 4:00 PM, jeka12386 <jeka12386 at hotmail.co.uk> wrote:> Dear all > > I am trying to calculate the value of n using uniroot. ?Here is the message > I am having: > > <<< > Error in uniroot(integ, lower = 0, upper = 1000, n) : > ?'interval' must be a vector of length 2 >>> > > Please ?would you be able to ?give me an indication on why I am having this > error message. > > Many thanks > > EXAMPLE BELOW: > > ## t ?= ?statistics test ?from t -distribution ?[-Inf,Inf] > ## df == degree of freedom > ###p <- p value > > diff <- 0.5 > > y <- function(t,n){ > > df <- 2*n-2 > ncp1 <- sqrt((diff^2*n)/2) > p <- 1- pt(t,df=df1) > test <- qt((1-p),df=df1,ncp=ncp1)*(1/sqrt(2)) > return(test) > } > > integ <- function(t,n){ > 1-integrate(y,lower=0,upper=3.6,n)$value -0.5 > } > > uniroot(integ,lower=0,upper=1000)Running this I get a different error: Error in f(x, ...) : argument "n" is missing, with no default Perhaps you need to define n somewhere? It's also not clear to me what happens to the "t" argument of integ? Is it supposed to be the y of integrate? Best, Michael> > > -- > View this message in context: http://r.789695.n4.nabble.com/Uniroot-error-message-with-in-intergration-tp4634247.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
I have defined t at the beginning of my query. I have added n on uniroot below and still getting the same error message uniroot(integ,lower=0,upper=1000,n) -- View this message in context: http://r.789695.n4.nabble.com/Uniroot-error-message-with-in-intergration-tp4634247p4634264.html Sent from the R help mailing list archive at Nabble.com.
> Error in uniroot(integ, lower = 0, upper = 1000, n) : > 'interval' must be a vector of length 2 >>>> Please would you be able to give me an indication on why I am having this > error message.Because uniroot has a second parameter called 'interval' which overrides lower and upper and you have given uniroot a scalar called n as the first non-named argument. R has used its usual positional matching rules, which say that "Any unmatched formal arguments are bound to unnamed supplied arguments, in order." your first unmatched formal argument (interval) has been matched to your first unnamed argument (n) so uniroot has tried to use n as your interval. You need to add your own parameters as named parameters; eg if your function took something called N, you would specify N=n S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Many thanks Ellison I have modified it as you suggested but I have this error message <<<Error in f(lower, ...) : unused argument(s) (N = 54)>>> I am not sure which arguments I have missed?*y <- function(t,n){ diff <- 0.5 df1 <- 2*n-2 ncp1 <- sqrt((diff^2*n)/2) p <- 1- pt(t,df=df1) test <- qt((1-p),df=df1,ncp=ncp1)*(1/sqrt(2)) return(test) } integ <- function(n){ 1-integrate(y,lower=0,upper=2.7,n)$value -0.8 } uniroot(integ,lower=0,upper=1000,N=n) traceback()* -- View this message in context: http://r.789695.n4.nabble.com/Uniroot-error-message-with-in-intergration-tp4634247p4634278.html Sent from the R help mailing list archive at Nabble.com.