[R] what's wrong with this 'length' in function?

casperyc casperyc at hotmail.co.uk
Fri Nov 12 01:42:02 CET 2010


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.



More information about the R-help mailing list