Question

In: Statistics and Probability

How can I make my own function for rbinom(n, size, prob) in R. So, basically I...

How can I make my own function for rbinom(n, size, prob) in R.

So, basically I want to build my own rbinom function that gives me the same result as rbinom that's built in R software.

Solutions

Expert Solution

The function is below

R code with comments (all statements starting with # are comments)

#define the rbinom function
myRbinom<-function(n,size,prob){
   #draw n*size numbers from uniform random (0,1)
   u<-runif(n*size)
   #arrange this into a matrix of size (n x size): n rows and size columns
   u<-matrix(u,nrow=n,ncol=size)
   #set the values which are less than prob as success
   success<-u<prob
   #count the number of successes for each row
   x<-apply(success,1,sum)
   return(x)
}

## now let us try this out for number of trials = 10 and success probability =0.2.

We know that the theoretical mean for a Binomial distribution (or the expectation) is trials*prob = 10*0.2=2

the theoretical variance is trials*prob*(1-prob) = 10*0.2*0.8=1.6

Let us see if we can get these values

R code

#set the random seed
set.seed(123)
n<-10000
size<-10
p<-0.2
y<-myRbinom(n,size,p)
#Check if this looks good
sprintf('Estimated mean of y is %.4f',mean(y))
sprintf('Estimated variance of y is %.4f',var(y))
sprintf('The theoretical mean of Y (E[Y]) is %.4f',size*p)
sprintf('The theoretical variance of Y is %.4f',size*p*(1-p))

## get this output

the estimated mean and variances are close to the theoretical values. Hence the function looks good.


Related Solutions

I JUST WANT AN EXAMPLE ANSWER SO I CAN MAKE MY OWN. THANK YOU!! Conduct research...
I JUST WANT AN EXAMPLE ANSWER SO I CAN MAKE MY OWN. THANK YOU!! Conduct research on New York City's large soda ban. Pretend you work at the Paradise City attorney's office. Draft a law that restricts the sale of large sugary drinks in Paradise City. Make sure to define the types of drinks and types of sellers restricted by the law. Example Statute: Title XXXIV (Links to an external site.)Links to an external site. ALCOHOLIC BEVERAGES AND TOBACCO Chapter...
I need a short and clear opinion so I can understand the question and make my...
I need a short and clear opinion so I can understand the question and make my own opinion Writing in the 19th century, the British economist Thomas Malthus argued that the law of diminishing marginal returns implied eventual misery for humanity because, in the case of agriculture, there is a fixed amount of available land and additional labor would only yield ever smaller increases in food production. The inevitable result, as Malthus saw it, was that population growth would eventually...
Here is my C++ program so far. Is there anyway I can make it display an...
Here is my C++ program so far. Is there anyway I can make it display an error if the user enters a float? Thanks #include <iostream> using namespace std; // Creating a constant for the number of integers in the array const int size = 10; int main() { // Assigning literals to the varibles int a[size]; int sum=0; float avg; // For loop that will reiterate until all 10 integers are entered by the user for(int i=0; i<size; i++)...
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
NOTE: I am just checking my own work so if you respond, please make sure I...
NOTE: I am just checking my own work so if you respond, please make sure I can read your handwriting. A loan officer wants to compare the interest rates for 48-month fixed-rate auto loans and 48-month variable-rate auto loans. Two independent, random samples of auto loans are selected. A sample of eight 48-month fixed-rate auto-loans have the following loan rates: 4.29%, 3.75%, 3.50%, 3.99%, 3.75%, 3.99%, 5.40%, 4.00% while a sample of five 48-month variable-rate auto loans have the loan...
function[prob,flop]=Matlab(a,b) a=[1,2,3]; b=[7,8,9]; if (length(a)~=length(b)) disp("it doesn't make sense"); end prob=0; flop=0; for i=1:length(a) prob=prob+a(i)*b(i); flop=flop+2;...
function[prob,flop]=Matlab(a,b) a=[1,2,3]; b=[7,8,9]; if (length(a)~=length(b)) disp("it doesn't make sense"); end prob=0; flop=0; for i=1:length(a) prob=prob+a(i)*b(i); flop=flop+2; end disp(prob); disp(flop); What is wrong with my code? it only run the disp(prob) and it doesn't run the disp(flop) here is what I got ans = 50 However, if I take % for the function line, it shows 50 6
How to proof: Matrix A have a size of m×n, and the rank is r. How...
How to proof: Matrix A have a size of m×n, and the rank is r. How can we rigorous proof that the dimension of column space are always equal to the dimensional of row space? (I can use many examples to show this work, but how to proof rigorously?)
My question is if n<r when there is no loop. I can't enter numbers again.Where is...
My question is if n<r when there is no loop. I can't enter numbers again.Where is the problem? How do it? #include int fact(int no) { if (no == 0) return 1; return no * fact(no - 1); } int calc(int n, int r,int *com,int *per) //as given in question formula of permutation n!/(n-r)! { *com=fact(n)/(fact(r)*fact(n-r)); *per=fact(n)/fact(n-r); } int main() { int n=0,r=0,co,pe; while(n<=0 || r<=0) { printf("Enter N value:"); scanf("%d",&n); printf("Enter R value:"); scanf("%d",&r); if(n { printf("N value should...
Maslows Theory: your ability to set my own needs and how i can set goals to...
Maslows Theory: your ability to set my own needs and how i can set goals to acheive them
Can I get a detailed answer going threw all the steps so I can check my...
Can I get a detailed answer going threw all the steps so I can check my answer. A total debt of $ 1,000 due now, $4000 due 2 years from now, and $6000 due 5 years from now is to be repaid by 3 payments. (1) The first payment is made now. (2) The second payment, which is 80% of the first, is made at the end of 30 months from now. (3) The third payment, which is 60% of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT