Error in n * rvec : non-numeric argument to binary operator

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Error in n * rvec : non-numeric argument to binary operator

anikaM
Hello,

I have this code, and when I run it:
> kbpowerf()
Error in n * rvec : non-numeric argument to binary operator

this is the code:

function (){
  #USER SPECIFICATION PORTION
  alpha=0.05 #DESIGNATED ALPHA
  g=3 #NUMBER OF GROUPS
  nvec=c(25,10,15) #GROUP SIZES
  beta1vec=c(789.93,122.87,1871.99) #SLOPE COEFFICIENTS
  sigsq=209460.57 #ERROR VARIANCE
  tausqvec=c(0.1596,0.1602,0.1360) #PREDICTOR VARIANCES
  #END OF SPECIFICATION
  df1<-g-1
  numint<-200
  l<-numint+1
  dd<-1e-6
  coevec<-c(1,rep(c(4,2),numint/2-1),4,1)
  set.seed(2017)
  repn<-20000
  kbpowerf<-function(){
    df<-sum(nvec)-2*g
    fcrit<-qf(1-alpha,df1,df)
    dfkvec<-nvec-1
    dfk<-sum(dfkvec)
    cl<-dd
    cu<-qchisq(1-dd,dfk)
    intc<-(cu-cl)/numint
    cvec<-cl+intc*(0:numint)
    wcpdf<-(intc/3)*coevec*dchisq(cvec,dfk)
    dfb1<-cumsum(dfkvec[1:g-1])
    dfb2<-dfkvec[2:g]
    ep<-0
    for (i in seq(repn)) {
      bvec<-rbeta(df1,dfb1/2,dfb2/2)
      avec<-rep(0,g)
      avec[1]<-exp(sum(log(bvec)))
      for (ig in (2:df1)) {
        avec[ig]<-(1-bvec[ig-1])*exp(sum(log(bvec[ig:df1])))
      }
      avec[g]<-1-bvec[df1]
      oavec<-tausqvec*avec
      buw<-sum(oavec*beta1vec)/sum(oavec)
      lamvec<-cvec*sum(oavec*(beta1vec-buw)^2)/sigsq
      ep<-ep+sum(wcpdf*pf(fcrit,df1,df,lamvec,lower.tail=FALSE))
    }
    kbpower<-ep/repn
  }
  kbpower<-kbpowerf()
  return(list(sample_size=nvec,attained_power=kbpower))
}

Can you please advise,
Thanks
Ana

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Error in n * rvec : non-numeric argument to binary operator

Rolf Turner

On Mon, 19 Apr 2021 17:01:11 -0500
Ana Marija <[hidden email]> wrote:

> Hello,
>
> I have this code, and when I run it:
> > kbpowerf()
> Error in n * rvec : non-numeric argument to binary operator
>
> this is the code:
>
> function (){
>   #USER SPECIFICATION PORTION
>   alpha=0.05 #DESIGNATED ALPHA
>   g=3 #NUMBER OF GROUPS
>   nvec=c(25,10,15) #GROUP SIZES
>   beta1vec=c(789.93,122.87,1871.99) #SLOPE COEFFICIENTS
>   sigsq=209460.57 #ERROR VARIANCE
>   tausqvec=c(0.1596,0.1602,0.1360) #PREDICTOR VARIANCES
>   #END OF SPECIFICATION
>   df1<-g-1
>   numint<-200
>   l<-numint+1
>   dd<-1e-6
>   coevec<-c(1,rep(c(4,2),numint/2-1),4,1)
>   set.seed(2017)
>   repn<-20000
>   kbpowerf<-function(){
>     df<-sum(nvec)-2*g
>     fcrit<-qf(1-alpha,df1,df)
>     dfkvec<-nvec-1
>     dfk<-sum(dfkvec)
>     cl<-dd
>     cu<-qchisq(1-dd,dfk)
>     intc<-(cu-cl)/numint
>     cvec<-cl+intc*(0:numint)
>     wcpdf<-(intc/3)*coevec*dchisq(cvec,dfk)
>     dfb1<-cumsum(dfkvec[1:g-1])
>     dfb2<-dfkvec[2:g]
>     ep<-0
>     for (i in seq(repn)) {
>       bvec<-rbeta(df1,dfb1/2,dfb2/2)
>       avec<-rep(0,g)
>       avec[1]<-exp(sum(log(bvec)))
>       for (ig in (2:df1)) {
>         avec[ig]<-(1-bvec[ig-1])*exp(sum(log(bvec[ig:df1])))
>       }
>       avec[g]<-1-bvec[df1]
>       oavec<-tausqvec*avec
>       buw<-sum(oavec*beta1vec)/sum(oavec)
>       lamvec<-cvec*sum(oavec*(beta1vec-buw)^2)/sigsq
>       ep<-ep+sum(wcpdf*pf(fcrit,df1,df,lamvec,lower.tail=FALSE))
>     }
>     kbpower<-ep/repn
>   }
>   kbpower<-kbpowerf()
>   return(list(sample_size=nvec,attained_power=kbpower))
> }
>
> Can you please advise,

There is no "rvec" anywhere in the code that you provided, so the error
is coming from somewhere else.

I ran your code and got:

> $sample_size
> [1] 25 10 15
>
> $attained_power
> [1] 0.8404461

No error.

Perhaps try running your code in a "clean" workspace.

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.