Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve the following project in R . Solve such that the code function will be able to calculate and plot histogram as output. Project s

Solve the following project in R. Solve such that the code function will be able to calculate and plot histogram as output.
Projects description
This is a little fun project, and short, too. The project aims to define a class that allows the creation of new probability distributions based on existing ones. The solution needs to be a S3 class named randomVariate. The constructor of the class must accept two arguments. The first one, named distFormula, should be a formula, e.g., ~ x + y, describing the function of one or more random variables. The second argument, named defs, should be a list of functions used for sampling from given distributions with keys corresponding to symbols used in the formula. The created object of the class randomVariate should be a function used for sampling from the distribution defined by the formula. You should also write the print method specific to this class. The following examples should clarify the concept.
Example 1
Let's start with a simple example. Let
be a standard normal random variable
. The function used for sampling from this distribution in the R language is rnorm(). Suppose we want to define a new random variable
based on the random variable
. Specifically, we want a function that allows for generating random numbers for this new random variable
. We can define this new function using the constructor of the class randomVariate as follows.
### definition of the new variable
newDist <- randomVariate(
distFormula = ~ x^2,
defs = list(
x = rnorm
)
)
In the above snippet, we defined a new random variable
. We used the constructor of the class randomVariate. The first argument of the constructor is a formula describing the function of the random variable
. The second argument is a list defining the distribution of the random variable
. Here, it is a standard normal distribution. The constructor creates a new object of the class randomVariate. We can print it out to see the mathematical definition of this new object.
newDist
output:
Formula
~x^2
Definitions
x
------------
function (n, mean =0, sd =1)
.Call(C_rnorm, n, mean, sd)
The object is printed in a specific way. First, a formula is printed, and then all used definitions. Here, there is only one definition for the symbol x. In this particular case, the definition is a function rnorm(), which is just a call to a compiled function C_rnorm().
We can use the new object as a regular function to sample random numbers from the new distribution. The following snippet creates a random sample and plots a histogram based on the sample.
sample1<- newDist(10^5)
hist(sample1, breaks = "Scott", probability = TRUE,
col = rgb(1,0,1,.2), density =20, angle =-45)
lines(
density(sample1, adjust =.5),
col = rgb(1,0,1,1))
The above snippet generates the following graph. The new variable takes only non-negative values.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

e. What are notable achievements of the group?

Answered: 1 week ago