0% found this document useful (0 votes)
51 views2 pages

R Functions for Statistical Calculations

The document outlines a series of programming tasks prepared by Rakesh Gupta, focusing on functions related to statistical calculations in R. It includes solutions for finding the absolute value of an integer, counting values larger than the mean, calculating binomial probabilities, cumulative probabilities for binomial and exponential distributions, and determining a threshold value for exponential distributions. Each task is accompanied by example code demonstrating the implementation of the required functions.

Uploaded by

sarthakgarg0401
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

R Functions for Statistical Calculations

The document outlines a series of programming tasks prepared by Rakesh Gupta, focusing on functions related to statistical calculations in R. It includes solutions for finding the absolute value of an integer, counting values larger than the mean, calculating binomial probabilities, cumulative probabilities for binomial and exponential distributions, and determining a threshold value for exponential distributions. Each task is accompanied by example code demonstrating the implementation of the required functions.

Uploaded by

sarthakgarg0401
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

FUNCTION Prepared by Rakesh gupta

Question 1: Write a function that returns the absolute value of an integer vector x of length one.

Question 2: Write a function that returns the number of values that are larger than the mean of a vector. You are allowed to use mean().

Question 3: Write a function to get the probability of P(X=x), if X ~ Bin (n , p).

Question 4: Write a function to get the Cumulative probability of X and Y, if X~Bin(n,p) and Y~ Exp(lambda) .

Question 5: Write a function to get value of a, when P(X<a) = p , when X ~ Exp(lambda)


FUNCTION Prepared by Rakesh gupta
Exercise 1
x <- -10
abs <- x
if (x < 0) {
abs = -x
}
cat("The absolute value of ", x, " is ", abs , "\n" )

Solution 2

x <- c(-100, 10, 20, 30, 50, 51, 52, 53, 54, 55)
counter <- 0
mean <- mean(x)
for (i in 1:length(x)){
if(x[i] > mean){
counter <- counter +1
}
}
cat("The number of values that are bigger than the mean is",
counter, "\n")
## The number of values that are bigger than the mean is 7

Solution 3:

db<-function(x,n,p){choose(n,x)*p^(x)*(1-p)^(n-x)}

Db(5,10,0.5)
[1] 0.2460938

Solution 4:

> Cb<-function(x,n,p){ s<-0


+ for(i in 0:x){
+ y<- choose(n,i)*p^(i)*(1-p)^(n-i)
+ s<-s+y
+ }
+ return(s)
+ }
> Cb(5,10,0.5)
[1] 0.6230469

Solution 5:

expin<-function(p,l){(-1/l)*log(1-p)}
> expin(0.5,2)
[1] 0.3465736

You might also like