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?)
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...
LED headlights on cars seem to becoming increasingly popular, so I thought I would make my...
LED headlights on cars seem to becoming increasingly popular, so I thought I would make my problem about that. I believe that the proportion of car owners who have LED headlights is higher than the proportion of car owners to use regular non-LED headlights now, (according to data from 2019) versus in 2010. During two independent surveys, one survey noted that 355 out of 600 car owners had LED headlights in 2020, while in 2010, 250 out of 600 car...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT