You change x from a single value to a vector of size 2, for example here:
if (j==2) {x=x+c(-1,1)*0.5}
That makes
c( qchisq(1-alpha/2,df=2*x)/2,
qchisq(alpha/2,df=2*x+2)/2)
a vector of 4 numbers that you are trying to assign to a row of a
matrix with two columns.
Keep x a single number and things will run smoothly.
Peter
On Thu, Nov 11, 2010 at 4:42 PM, casperyc <casperyc at hotmail.co.uk>
wrote:>
> Hi all,
>
> I am having a trouble with this function I wrote
>
> ###################################################
> p26=function(x,alpha){
>
> ? ? ? ?# dummy variable
> ? ? ? ?j=1
>
> ? ? ? ?ci=matrix(ncol=2,nrow=3)
>
> ? ? ? ?while (j<4){
> ? ? ? ? ? ? ? ?if (j==2) {x=x+c(-1,1)*0.5}
>
> ? ? ? ? ? ? ? ?ci[j,]> ? ? ? ? ? ? ? ?x+qnorm(1-alpha/2)^2/2+
> ? ? ? ? ? ? ? ?c(-1,1)*qnorm(1-alpha/2)*
> ? ? ? ? ? ? ? ?sqrt(x+qnorm(1-alpha/2)^2/4)
>
> ? ? ? ? ? ? ? ?j=j+1
>
> ? ? ? ? ? ? ? ?if (j==3) { # exact
> ? ? ? ? ? ? ? ? ? ? ? ?x=x-c(-1,1)*0.5
> ? ? ? ? ? ? ? ? ? ? ? ?ci[j,]=c(
> ? ? ? ? ? ? ? ? ? ? ? ?qchisq(1-alpha/2,df=2*x)/2,
> ? ? ? ? ? ? ? ? ? ? ? ?qchisq(alpha/2,df=2*x+2)/2)
> ? ? ? ? ? ? ? ?j=j+1
> ? ? ? ? ? ? ? ?}
> ? ? ? ?}
> ? ? ? ? ? ? ?
?rownames(ci)=c('without','with','exact')
> ? ? ? ? ? ? ? ?colnames(ci)=c('lower','upper')
> ? ? ? ? ? ? ? ?return(round(ci,2))
> }
> ###################################################
>
> Most bits are fine.
>
> The problem part is
> ###################################################
> ? ? ? ? ? ? ? ?if (j==3) { # exact
> ? ? ? ? ? ? ? ? ? ? ? ?x=x-c(-1,1)*0.5
> ? ? ? ? ? ? ? ? ? ? ? ?ci[j,]=c(
> ? ? ? ? ? ? ? ? ? ? ? ?qchisq(1-alpha/2,df=2*x)/2,
> ? ? ? ? ? ? ? ? ? ? ? ?qchisq(alpha/2,df=2*x+2)/2)
> ? ? ? ? ? ? ? ?j=j+1
> ? ? ? ? ? ? ? ?}
> ###################################################
> in the middle, when I run the function with
>
> p26(89,0.05)
>
> I got the following
>
> ###################################################
> Error in ci[j, ] = c(qchisq(1 - alpha/2, df = 2 * x)/2, qchisq(alpha/2, ?:
> ?number of items to replace is not a multiple of replacement length
> ###################################################
>
> I have been looking at it for a long time, still dont know why the
'length'
> differ??
>
> can someone spot it?
>
> Thanks.
>
> casper
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/what-s-wrong-with-this-length-in-function-tp3038908p3038908.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.
>