NAME : KSHITIJ SINGH
ROLL NO. 2023/09/089
SUBJECT :- SEC: STATISTICS WITH R
ASSIGNMENT
Question 1.)
#In R, the pnorm() function computes the cumulative probability for a
normal distribution.
> # a. P(X < 80)
> pnorm(80, mean=100, sd=8)
[1] 0.006209665
>
> # b. P(X > 112)
> 1 - pnorm(112, mean=100, sd=8)
[1] 0.0668072
>
> # c. P(88 ≤ X ≤ 120)
> pnorm(120, mean=100, sd=8) - pnorm(88, mean=100, sd=8)
[1] 0.9269831
Question 2.)
> #In R, the pbinom() function computes cumulative probabilities for a
binomial distribution.
>
> # a. P(X > 12), which is P(X ≥ 13) since X is discrete
> 1 - pbinom(12, size=24, prob=0.6)
[1] 0.7869782
>
> # b. P(X ≤ 14)
> pbinom(14, size=24, prob=0.6)
[1] 0.5109198
>
# c. P(2 ≤ X ≤ 13)
> pbinom(13, size=24, prob=0.6) - pbinom(1, size=24, prob=0.6)
[1] 0.3497619
Question 4.)
> #rnorm(n, mean, sd) generates random numbers from a normal distribution.
> #rpois(n, lambda) generates random numbers from a Poisson distribution.
> #runif(n, min, max) generates random numbers from a uniform
distribution.
>
> # a. Normal Distribution N(μ=5, σ=1)
> rnorm(30, mean=5, sd=1)
[1] 3.600147 5.081025 5.178280 5.610464 5.773445 5.682718 5.365966
4.691245
[9] 5.203337 5.846225 3.337359 5.385883 4.700123 4.112172 3.664987
4.527830
[17] 5.218971 5.108499 5.496570 4.654222 5.731480 6.492030 5.074208
5.284704
[25] 4.057322 5.818772 3.891641 4.650325 4.683165 5.079878
>
> # b. Poisson Distribution P(λ=10)
> rpois(30, lambda=10)
[1] 4 9 15 14 4 8 5 8 3 7 11 9 4 5 8 11 9 10 9 9 6 10 10
12 6
[26] 13 12 11 7 7
>
> # c. Uniform Distribution U(a=0, b=10)
> runif(30, min=0, max=10)
[1] 4.2358718 8.2081805 2.5442867 3.2417167 7.3920686 5.8786666 5.3642412
[8] 2.9599005 4.7992341 4.1263761 2.6899658 0.7970676 1.9531729 5.7520545
[15] 3.9435540 7.2187177 1.0804011 7.5290178 3.3294188 9.0949020 3.5186056
[22] 3.1869991 4.2268628 6.2480709 5.8407220 5.8482934 4.9205602 7.0912380
[29] 5.4330078 5.9549733
Question 5.)
> #p creates the vector: [32, 54, 38, 44].
> #x creates the sequence: [10, 20, 30, 40, 50, 60, 70].
> #cut(p, breaks=x) categorizes each value into intervals defined by x
>
> p <- c(32, 54, 38, 44)
> p
[1] 32 54 38 44
> x <- seq(10, 70, by=10)
> x
[1] 10 20 30 40 50 60 70
> y <- cut(p, breaks=x)
> y
[1] (30,40] (50,60] (30,40] (40,50]
Levels: (10,20] (20,30] (30,40] (40,50] (50,60] (60,70]
Question 6.)
> #dbinom(x, size, prob) computes the probability mass function (PMF) for
a binomial distribution.
> #0:5 generates a vector of values from 0 to 5.
> # Calculate P(X = x) for x = 0 to 5
> dbinom(0:5, size=24, prob=0.6)
[1] 2.814750e-10 1.013310e-08 1.747960e-07 1.922756e-06 1.514170e-05
[6] 9.085020e-05
Question 7.)
> #For the first output, seq(2, 10, by=2) generates [2, 4, 6, 8, 10], and
rep(..., 2) repeats it twice.
> #For the second output, c() combines the character strings into a
vector.
> # First output: [1] 2 4 6 8 10 2 4 6 8 10
> c(2, 4, 6, 8, 10, 2, 4, 6, 8, 10)
[1] 2 4 6 8 10 2 4 6 8 10
>
> # Second output: [1] "3" "5" "7" "Mon" "Tue" "Wed"
> c("3", "5", "7", "Mon", "Tue", "Wed")
[1] "3" "5" "7" "Mon" "Tue" "Wed"
>
Question 8.)
> #rnorm() generates the sample.
> #mean() computes the sample mean.
> #Standard error is SampleSd/sqrt(n), where n=25
> # Generate random sample
> sample <- rnorm(25, mean=100, sd=12)
> sample
[1] 111.56035 105.95983 83.79301 103.33962 112.17337 91.90596 111.85070
[8] 86.99498 103.07496 93.27841 101.21175 95.51968 100.45139 111.94420
[15] 86.64928 102.18031 77.26946 98.95704 113.01832 100.77298 91.00802
[22] 125.06592 104.64171 108.85630 100.60613
>
> # Calculate mean
> M <- mean(sample)
> M
[1] 100.8833
>
> # Calculate standard error
> S <- sd(sample) / sqrt(25)
> S
[1] 2.177969
Question 9.)
a. scan(): Reads data into R from a file or the console interactively.
It’s often used to input raw numeric or character data.
b. structure(): Assigns attributes (e.g., names, dimensions) to an object,
such as converting a vector into a matrix or adding labels.
c. head(): Displays the first 6 rows (by default) of a data frame, matrix,
or vector, useful for previewing data.
d. tail(): Displays the last 6 rows (by default) of a data frame, matrix,
or vector, helpful for checking the end of a dataset.