I'm new to R and I'm simulating an experiment to apply some theoretical result. The experiment is this: At Larry storehouse, the number of customers is poisson distributed with parameter lambda
. The probability that a customer would buy a suit is p
. What is the probability that there will be k
suites sold?
This is my proposed codes but something is wrong. Please help!
Larry<-function(reps=10000, p=1/5, lambda=5, k=2){
sold<-c(rep(0, reps))
k_sold<-0
r<-rpois(reps,lambda)
for(i in 1:reps){
for(j in 1:r[i]){
sold[i]<-sold[i]+rbinom(1,1,p)
}
k_sold<- if(sold[i]==k){ k_sold+1}
}
}
cat("The probability that there are", k ,"clothes sold in Larry's store is", k_sold/reps, "\n")
return(list(sold))
}
Why R say that objects k_sold
and sold
are not found?