5. Performing Statistics R- II
5) Write an R program to implement time series analysis for the given data
CODE:
#Write an R program to perform time-series #analysis for the given data. #weekly data of Covid-19 Postive cases from #22 January to 15 April x=c(580,7813,28266,59287,75700,87820,95314,126214, 218843,471497,936851,1508725,2072113) #Library required for decimal_data function library(lubridate) # output to be created as image png(file="timeseries.png") # creating time series object # from date 22 January mts=ts(x,start=decimal_date(ymd("2020-01-22")), frequency = 365.25/7) plot(mts,xlab="Weekly Data",ylab="Total postive cases", main="Covid-19 pandemic",col.main="darkgreen") #saving the file dev.off()
OUTPUT :
5B) Write an R program to implement.
i) Normal Distribution. [Hint: dnorm(), pnorm(), qnorm(), rnorm()]
ii) Binomial Distribution. [Hint: dbinom(), pbinom(), qbinom(), rbinom()]
CODE:
# Normal Distribution set.seed(123) # Set seed for reproducibility # Parameters for the normal distribution mean_value <- 0 sd_value <- 1 # Generate random samples from a normal distribution random_samples <- rnorm(1000, mean = mean_value, sd = sd_value) # Calculate probability density function (PDF) at specific points pdf_values <- dnorm(random_samples, mean = mean_value, sd = sd_value) # Calculate cumulative distribution function (CDF) at specific points cdf_values <- pnorm(random_samples, mean = mean_value, sd = sd_value) # Quantile function (inverse CDF) - Find the value corresponding to a given probability quantile_value <- qnorm(0.95, mean = mean_value, sd = sd_value) cat("Normal Distribution:\n") cat("Quantile (Inverse CDF) at probability 0.95:", quantile_value, "\n\n") # Binomial Distribution # Parameters for the binomial distribution n_trials <- 10 prob_success <- 0.5 # Generate random samples from a binomial distribution binom_samples <- rbinom(1000, size = n_trials, prob = prob_success) # Calculate probability mass function (PMF) at specific points pmf_values <- dbinom(binom_samples, size = n_trials, prob = prob_success) # Calculate cumulative distribution function (CDF) at specific points cdf_binom_values <- pbinom(binom_samples, size = n_trials, prob = prob_success) # Quantile function (inverse CDF) - Find the value corresponding to a given probability quantile_binom_value <- qbinom(0.95, size = n_trials, prob = prob_success) cat("Binomial Distribution:\n") cat("Quantile (Inverse CDF) at probability 0.95:", quantile_binom_value, "\n")
OUTPUT :
Normal Distribution: Quantile (Inverse CDF) at probability 0.95: 1.644854 Binomial Distribution: Quantile (Inverse CDF) at probability 0.95: 8