statistics - What is causing my R script to return longer object length is not a multiple of shorter object length? -
here code:
production<-function(alpha1,alpha2,beta1,beta2,d1,d2){ alpha3=1-alpha1-alpha2 beta3=1-beta1-beta2 observation.1 <- c(p.path1=alpha1, p.path2=alpha1*alpha2, p.path3=alpha2, p.path4=alpha3,p.path5=alpha1, p.path6=alpha1*alpha2) revenue.1 <- c(c.path1=d1+d2,c.path2=d2,c.path3=d1,c.path4=d2+d1+d1,c.path5=d1+d2,c.path6=0) observation.2 <-c(p.path7=beta2,p.path8=beta1, p.path9=beta1*beta2,p.path10=beta3*beta1,p.path11=beta2,p.path12=beta1) revenue.2<- c(c.path7=d1+d1,c.path8=d2,c.path9=d2*2,c.path10=d1+d2,c.path11=d1+d1,c.path12=0) production1=sum(revenue.1*observation.1) production2=sum(revenue.2*observation.2) outcomes<-c(meancostproduction1 = production1, meancostproduction2=production2) } results<-production(alpha1=0.1,alpha2=0.9,beta1=0.33,beta2=0.67,d1=0.4,d2=0.6) print(results) #new values sample of parameters alpha1<-rbeta(12,1.5 , 0.5+ 80-42) alpha2<-rbeta(12,21.5 , 0.5+ 80-21) beta1<-rbeta(12, 33.5 , 0.5+ 92-33) beta2<-rbeta(12, 44.5 ,0.5+ 92-44) d1<-rlnorm(12,18,0.32) d2<-rlnorm(12,46,0.6) results<-production(alpha1,alpha2,beta1,beta2,d1,d2)
i don't understand why receiving 2 warning messages:
"warning messages: 1: in revenue.1 * observation.1 : longer object length not multiple of shorter object length 2: in revenue.2 * observation.2 : longer object length not multiple of shorter object length"
it working fine until:
results<-production(alpha1=0.1,alpha2=0.9,beta1=0.33,beta2=0.67,d1=0.4,d2=0.6) print(results)
however after updating values of parameters of production function, warning messages.
i noticed if c.path12
in revenue.2 not 0 , either d1 or d2, second warning message disappears.
could please point out error on code? many thanks
the reason you're passing 0
final argument c()
when constructing revenue.1
, you're passing alpha1*alpha2
final argument c()
when constructing observation.1
. expression alpha1*alpha2
vectorized multiplication result in product vector equal length of inputs (which have same size each other in calls of production()
). 0
not replicated length equal length of product vector, thus, when pass inputs production()
have length greater one, end length discrepancy between revenue.1
, observation.1
. causes warning message when try perform vectorized multiplication on these 2 operands in production1=sum(revenue.1*observation.1)
.
these warnings not occur on first call production()
because you're passing one-element vectors inputs in case, one-element 0
naturally has correct size call. in second call, input vectors length 12, revenue.1
ends being short (compared observation.1
) 11 elements.
you can solve replicating 0 sufficient number of times cover length of inputs:
revenue.1 <- c(c.path1=d1+d2,c.path2=d2,c.path3=d1,c.path4=d2+d1+d1,c.path5=d1+d2,c.path6=rep(0,length(alpha1)))
ditto construction of revenue.2
:
revenue.2 <- c(c.path7=d1+d1,c.path8=d2,c.path9=d2*2,c.path10=d1+d2,c.path11=d1+d1,c.path12=rep(0,length(alpha1)))
speaking more generally, warning r gives when try perform vectorized operation operands of different lengths, , shorter operand length not evenly divide longer operand length. if lengths are divisible, r silently "cycle" shorter operand match length of longer operand. here's demo:
1:2*1:2; ## equal lengths ## [1] 1 4 1:2*1:4; ## divisible lengths; short operand cycled long operand's length ## [1] 1 4 3 8 1:2*1:3; ## indivisible lengths; warning message, partial cycling ## [1] 1 4 3 ## warning message: ## in 1:2 * 1:3 : ## longer object length not multiple of shorter object length
Comments
Post a Comment