ARITHMETIC OPERATORS
#Arithmetic Operators
a <- 10
b <- 5
sum <- a + b # Addition
diff <- a - b # Subtraction
prod <- a * b # Multiplication
quot <- a / b # Division
mod <- a %% b # Modulus (Remainder)
power <- a ^ b # Exponentiation
cat("Arithmetic Operators:\n")
cat("Sum: ", sum, "\n")
cat("Difference: ", diff, "\n")
cat("Product: ", prod, "\n")
cat("Quotient: ", quot, "\n")
cat("Modulus: ", mod, "\n")
cat("Power: ", power, "\n\n")
# Relational Operators
greater <- a > b # Greater than
less <- a < b # Less than
equal <- a == b # Equal to
not_equal <- a != b # Not equal to
greater_equal <- a >= b # Greater than or equal to
less_equal <- a <= b # Less than or equal to
cat("Relational Operators:\n")
cat("Greater: ", greater, "\n")
cat("Less: ", less, "\n")
cat("Equal: ", equal, "\n")
cat("Not Equal: ", not_equal, "\n")
cat("Greater or Equal: ", greater_equal, "\n")
cat("Less or Equal: ", less_equal, "\n\n")
# Logical Operators
x <- TRUE
y <- FALSE
and_op <- x & y # AND operator
or_op <- x | y # OR operator
not_op <- !x # NOT operator
cat("Logical Operators:\n")
cat("AND: ", and_op, "\n")
cat("OR: ", or_op, "\n")
cat("NOT: ", not_op, "\n\n")
# Assignment Operators
variable1 <- 20 # Leftward assignment
variable2 <<- 30 # Global assignment
20 -> variable3 # Rightward assignment
cat("Assignment Operators:\n")
cat("Variable1: ", variable1, "\n")
cat("Variable2: ", variable2, "\n")
cat("Variable3: ", variable3, "\n")
OUTPUT:
ARRAY
data<- 1:18
my_array<- array(data, dim=c(3,3,2))
cat("ORIGINAL ARRAY:\n")
print(my_array)
element<-my_array[2,3,1]
cat("\n Element at position (2, 3,1):", element,"\n")
new_array<-my_array+5
cat("\n Array after adding 5 to each element: \n")
print(new_array)
sum_all_elements<- sum(new_array)
cat("\nSum of all elements in the array:\n", sum_all_elements,"\n")
OUTPUT:
> source("D:/R PROGRAM/array.r")
ORIGINAL ARRAY:
,,1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
,,2
[,1] [,2] [,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
Element at position (2, 3,1): 8
Array after adding 5 to each element:
,,1
[,1] [,2] [,3]
[1,] 6 9 12
[2,] 7 10 13
[3,] 8 11 14
,,2
[,1] [,2] [,3]
[1,] 15 18 21
[2,] 16 19 22
[3,] 17 20 23
Sum of all elements in the array:
261
LIST
my_list<- list(name="soniya",
age=30,scores= c(85,90,92),address=list(street="123 main st" ,
city ="anytown", zip="631501")
)
cat("Original List:\n")
print(my_list)
name<-my_list$name
cat("\nName:",name,"\n")
age<-my_list$age
cat("Age:", age, "\n")
scores<- my_list$scores
cat("Scores:", scores, "\n")
city<-my_list$address$city
cat("City:",city, "\n")
my_list$age<-31
my_list$scores<-c(88,91,95)
cat("\n Modified List:\n")
print(my_list)
average_score<-mean(my_list$scores)
cat("\nAverage Score:", average_score, "\n")
cat("\nFinal List: \n")
print(my_list)
OUTPUT:
source("D:/R PROGRAM/list.r")
Original List:
$name
[1] "soniya"
$age
[1] 30
$scores
[1] 85 90 92
$address
$address$street
[1] "123 main st"
$address$city
[1] "anytown"
$address$zip
[1] "631501"
Name: soniya
Age: 30
Scores: 85 90 92
City: anytown
Modified List:
$name
[1] "soniya"
$age
[1] 31
$scores
[1] 88 91 95
$address
$address$street
[1] "123 main st"
$address$city
[1] "anytown"
$address$zip
[1] "631501"
Average Score: 91.33333
Final List:
$name
[1] "soniya"
$age
[1] 31
$scores
[1] 88 91 95
$address
$address$street
[1] "123 main st"
$address$city
[1] "anytown"
$address$zip
[1] "631501"
BUILT-IN FUNCTION
numbers<- c(10,20,30,40,50)
string<-"Hello, World!"
sum_result<-sum(numbers)
mean_result<-mean(numbers)
sqrt_result<-sqrt(numbers)
toupper_result<-toupper(string)
substring_result<-substr(string,1,5)
min_result<-min(numbers)
max_result<-max(numbers)
sd_result<-sd(numbers)
cat("Sum of numbers:", sum_result, "\n")
cat("Mean of numbers:",mean_result, "\n")
cat("Square root of numbers:", sqrt_result, "\n")
cat("Uppercase string:",toupper_result, "\n")
cat("Substring of string:", substring_result, "\n")
cat("Minimum value :", min_result, "\n")
cat("Maximum value :", max_result, "\n")
cat("Standard deviation :", sd_result, "\n")
OUTPUT:
FUNCTIONS
add_numbers <-function(a,b)
{
return(a+b)
}
factorial<-function(n)
{
if(n==0)
{
return(1)
}
else
{
return(n*factorial(n-1))
}
}
reverse_string<-function(str)
{
return(paste(rev(strsplit(str,NuLL)[[1]]),collapse=""))
}
sum_result<-add_numbers(5,10)
factorial_result<-factorial(5)
reversed_string<-reverse_string("Hello WORLD!")
cat("Sum of 5 and 10:", sum_result, "\n")
cat("Factorial of 5 :", factorial_result, "\n")
cat("Reversed string:", reversed_string, "\n")
OUTPUT:
source("D:/R PROGRAM/rev.r")
Sum of 5 and 10: 15
Factorial of 5 : 120
Reversed string: !DLROW OLLEH
VECTORS
numeric_vector<-c(1,2,3,4,5)
character_vector<-c("apple","banana","cherry")
logical_vector<-c(TRUE,FALSE,TRUE,FALSE)
cat("Numeric Vector;\n")
print(numeric_vector)
cat("\nCharacter Vector:\n")
print(character_vector)
cat("\nLogical Vector:\n")
print(logical_vector)
numeric_element<-numeric_vector[2]
cat("\nElement at position 2 in numeric vector:", numeric_element,"\n")
character_element<-character_vector[3]
cat("\nElement at position 3 in numeric vector:", numeric_element,"\n")
numeric_vector<-numeric_vector*2
character_vetcor<-c(character_vector,"date")
cat("\nNumeric Vector after multiplication by 2 :\n")
print(numeric_vector)
cat("\nCharacter Vector after concatenation:\n")
print(character_vector)
sum_numeric_vector<-sum(numeric_vector)
mean_numeric_vector<-mean(numeric_vector)
cat("\nSum of numeric vector elements:",sum_numeric_vector,"\n")
cat("Mean of numeric vector elements:", mean_numeric_vector,"\n")
OUTPUT:
> source("D:/R PROGRAM/vector.r")
Numeric Vector;
[1] 1 2 3 4 5
Character Vector:
[1] "apple" "banana" "cherry"
Logical Vector:
[1] TRUE FALSE TRUE FALSE
Element at position 2 in numeric vector: 2
Element at position 3 in numeric vector: 2
Numeric Vector after multiplication by 2 :
[1] 2 4 6 8 10
Character Vector after concatenation:
[1] "apple" "banana" "cherry"
Sum of numeric vector elements: 30
Mean of numeric vector elements: 6
MATRIX ADDITION AND SUBTRACTION
# Define matrices
matrix1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2, ncol=3)
matrix2 <- matrix(c(6, 5, 4, 3, 2, 1), nrow=2, ncol=3)
# Matrix addition
matrix_sum <- matrix1 + matrix2
# Matrix subtraction
matrix_diff <- matrix1 - matrix2
cat("Matrix 1:\n")
print(matrix1)
cat("\nMatrix 2:\n")
print(matrix2)
cat("\nMatrix Sum (Addition):\n")
print(matrix_sum)
cat("\nMatrix Difference (Subtraction):\n")
print(matrix_diff)
OUTPUT:
> source("~/.active-rstudio-document")
Matrix 1:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Matrix 2:
[,1] [,2] [,3]
[1,] 6 4 2
[2,] 5 3 1
Matrix Sum (Addition):
[,1] [,2] [,3]
[1,] 7 7 7
[2,] 7 7 7
Matrix Difference (Subtraction):
[,1] [,2] [,3]
[1,] -5 -1 3
[2,] -3 1 5
MATRIX MULTIPLICATION AND TRANSPOSE OF A MATRIX
# Define matrices
matrix1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2, ncol=3)
matrix2 <- matrix(c(7, 8, 9, 10, 11, 12), nrow=3, ncol=2)
# Matrix multiplication
matrix_product <- matrix1 %*% matrix2
# Transpose of matrix1
transpose_matrix1 <- t(matrix1)
# Input (matrices used)
cat("Matrix 1:\n")
print(matrix1)
cat("\nMatrix 2:\n")
print(matrix2)
# OUTPUT: (results of operations)
cat("\nMatrix Product (Multiplication of Matrix 1 and Matrix 2):\n")
print(matrix_product)
cat("\nTranspose of Matrix 1:\n")
print(transpose_matrix1)
OUTPUT:
> source("D:/R PROGRAM/MULTI.r")
Matrix 1:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Matrix 2:
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
Matrix Product (Multiplication of Matrix 1 and Matrix 2):
[,1] [,2]
[1,] 76 103
[2,] 100 136
Transpose of Matrix 1:
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
Matrix 2:
> print(matrix2)
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
> # OUTPUT: (results of operations)
> cat("\nMatrix Product (Multiplication of Matrix 1 and Matrix 2):\n")
Matrix Product (Multiplication of Matrix 1 and Matrix 2):
> print(matrix_product)
[,1] [,2]
[1,] 76 103
[2,] 100 136
> cat("\nTranspose of Matrix 1:\n")
Transpose of Matrix 1:
> print(transpose_matrix1)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
Transpose of Matrix 2:
> print(transpose_matrix2)
[,1] [,2] [,3]
[1,] 7 8 9
[2,] 10 11 12
MEAN AND MEDIAN
# Define a numeric vector
numeric_vector <- c(4, 8, 15, 16, 23, 42)
# Compute the mean of the vector
mean_value <- mean(numeric_vector)
# Compute the median of the vector
median_value <- median(numeric_vector)
# Input (original numeric vector)
cat("Numeric Vector:\n")
print(numeric_vector)
# OUTPUT: (mean and median of the vector)
cat("\nMean of the Numeric Vector:\n")
print(mean_value)
cat("\nMedian of the Numeric Vector:\n")
print(median_value)
OUTPUT:
QUARTILE, RANGE AND INTER QUARTILE RANGE
# Define a numeric vector
numeric_vector <- c(4, 8, 15, 16, 23, 42)
# Compute quartiles
quartiles <- quantile(numeric_vector)
# Compute range
range_values <- range(numeric_vector)
range_value <- diff(range_values)
# Compute interquartile range (IQR)
iqr_value <- IQR(numeric_vector)
# Input (original numeric vector)
cat("Numeric Vector:\n")
print(numeric_vector)
# OUTPUT: (quartiles, range, and IQR of the vector)
cat("\nQuartiles of the Numeric Vector:\n")
print(quartiles)
cat("\nRange of the Numeric Vector:\n")
print(range_value)
cat("\nInterquartile Range (IQR) of the Numeric Vector:\n")
print(iqr_value)
OUTPUT:
MAXIMUM, MINIMUM AND PERCENTILE
x<- c(2, 13, 5, 36, 12, 50)
# Calculate the minimum
min_value <- min(x)
print(paste("Minimum:", min_value))
# Calculate the maximum
max_value <- max(x)
print(paste("Maximum:", max_value))
# Calculate percentiles (including quartiles)
percentiles <- quantile(x, probs = c(0, 0.25, 0.5, 0.75, 1))
print("Percentiles (0%, 25%, 50%, 75%, 100%):")
print(percentiles)
# Calculate specific percentiles (e.g., 10th, 50th, and 90th)
specific_percentiles <- quantile(x, probs = c(0.10, 0.50, 0.90))
print("Specific Percentiles (10%, 50%, 90%):")
print(specific_percentiles)
OUTPUT:
[1] "Minimum: 2"
[1] "Maximum: 50"
[1] "Percentiles (0%, 25%, 50%, 75%, 100%):"
0% 25% 50% 75% 100%
2.00 6.75 12.50 30.25 50.00
[1] "Specific Percentiles (10%, 50%, 90%):"
10% 50% 90%
4.4 12.5 44.4
STANDARD DEVIATION, VARIANCE AND CO-VARIANCE
x <- c(2, 13, 5, 36, 12, 50)
y <- c(3, 15, 8, 39, 10, 45)
# Calculate the variance of x
variance_x <- var(x)
print(paste("Variance of x:", variance_x))
# Calculate the standard deviation of x
std_dev_x <- sd(x)
print(paste("Standard Deviation of x:", std_dev_x))
# Calculate the covariance between x and y
covariance_xy <- cov(x, y)
print(paste("Covariance between x and y:", covariance_xy))
# Calculate the variance of y (optional)
variance_y <- var(y)
print(paste("Variance of y:", variance_y))
# Calculate the standard deviation of y (optional)
std_dev_y <- sd(y)
print(paste("Standard Deviation of y:", std_dev_y))
OUTPUT:
[1] "Variance of x: 330.3"
[1] "Standard Deviation of x: 18.17407"
[1] "Covariance between x and y: 378.9"
[1] "Variance of y: 379.7"
[1] "Standard Deviation of y: 19.48456"
CENTRAL MOMENTS, SKEWNESS AND KURTOSIS
x <- c(2, 13, 5, 36, 12, 50)
# Calculate mean
mean_x <- mean(x)
print(paste("Mean of x:", mean_x))
# Calculate the central moments
# 1st central moment (mean deviation from mean) is always 0, so we start from the 2nd
central moment
central_moment_2 <- mean((x - mean_x)^2)
central_moment_3 <- mean((x - mean_x)^3)
central_moment_4 <- mean((x - mean_x)^4)
print(paste("2nd Central Moment (Variance):", central_moment_2))
print(paste("3rd Central Moment:", central_moment_3))
print(paste("4th Central Moment:", central_moment_4))
# Calculate skewness
skewness_x <- skewness(x)
print(paste("Skewness of x:", skewness_x))
# Calculate kurtosis
kurtosis_x <- kurtosis(x)
print(paste("Kurtosis of x:", kurtosis_x))
# For comparison: standard kurtosis (subtract 3 to get excess kurtosis)
excess_kurtosis_x <- kurtosis(x) - 3
print(paste("Excess Kurtosis of x:", excess_kurtosis_x))
OUTPUT:
[1] "Mean of x: 19.6666666666667"
[1] "2nd Central Moment (Variance): 330.266666666667"
[1] "3rd Central Moment: 1160.59259259259"
[1] "4th Central Moment: 42938.8888888889"
[1] "Skewness of x: 0.529923953737746"
[1] "Kurtosis of x: 2.03527791255004"
[1] "Excess Kurtosis of x: -0.964722087449958"
CORRELATION COEFFICIENT
# Define or load the data
# Example: Using built-in mtcars dataset
data <- mtcars
x <- data$mpg
y <- data$wt
# Compute the correlation coefficient
correlation_coefficient <- cor(x, y)
# Print the result
print(paste("Correlation coefficient between mpg and wt:", correlation_coefficient))
OUTPUT:
BOX PLOT, SCATTER PLOT, PAIR PLOT
BOXPLOT
# Load the dataset
data(mtcars)
# Create the box plot
boxplot(disp ~ gear, data = mtcars,
main = "Displacement by Gear",
xlab = "Gear",
ylab = "Displacement")
OUTPUT:
SCATTER PLOT
# Simple Scatterplot
attach(mtcars)
plot(wt, mpg, main="Scatterplot Example",
xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
OUTPUT:
PAIR PLOT
# create sample_data
x <- rnorm(500)
y <- x + rnorm(500, 0, 10)
z <- x - rnorm(500, 0, 7)
sample_data <- data.frame(x, y, z)
#create pairs plot
pairs( sample_data )
OUTPUT:
LINE CHART, BAR CHART
Line chart
# Create the data for the chart.
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)
# Give the chart file a name.
png(file = "line_chart_2_lines.jpg")
# Plot the bar chart.
plot(v,type = "o",col = "red", xlab = "Month", ylab = "Rain fall",
main = "Rain fall chart")
lines(t, type = "o", col = "blue")
# Save the file.
dev.off()
OUTPUT:
BAR CHART
# defining vector
x <- c(7, 15, 23, 12, 44, 56, 32)
# OUTPUT: to be present as PNG file
png(file = "barplot.png")
# plotting vector
barplot(x, xlab = "GeeksforGeeks Audience",
ylab = "Count", col = "white",
col.axis = "darkgreen",
col.lab = "darkgreen")
# saving the file
dev.off()
OUTPUT:
LINEAR REGRESSION
data <- mtcars
# Define the variables
# Predicting 'mpg' (miles per gallon) using 'wt' (weight)
x <- data$wt
y <- data$mpg
# 1. Fit Linear Model
# Create a linear regression model
model <- lm(y ~ x)
# 2. Summarize Model
# Get detailed statistics of the model
summary(model)
# 3. Visualize Model
# Plot the data points
plot(x, y, main="Linear Regression of MPG on Weight",
xlab="Weight (1000 lbs)", ylab="Miles Per Gallon", pch=19, col="blue")
# Add the regression line
abline(model, col="red")
# Save the plot to a file (optional)
# You can uncomment the following lines to save the plot as an image
# png("linear_regression.png")
# plot(x, y, main="Linear Regression of MPG on Weight",
# xlab="Weight (1000 lbs)", ylab="Miles Per Gallon", pch=19, col="blue")
# abline(model, col="red")
# dev.off()
OUTPUT:
Residuals:
Min 1Q Median 3Q
Max -4.5432 -2.3647 -0.1252 1.4096 6.8727
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
x -5.3445 0.5591 -9.559 1.29e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
LOGISTIC REGRESSION
# Load the data
data <- read.csv("your_dataset.csv") # Replace with your dataset path
# Preview the data
str(data)
summary(data)
# Split the data into training and test sets
set.seed(123) # For reproducibility
trainIndex <- createDataPartition(data$Outcome, p = 0.7, list = FALSE) # Adjust 'Outcome' to
your dependent variable
trainData <- data[trainIndex, ]
testData <- data[-trainIndex, ]
# Fit the logistic regression model
model <- glm(Outcome ~ ., data = trainData, family = binomial)
# Summarize the model
summary(model)
# Make predictions on the test set
predictions <- predict(model, newdata = testData, type = "response")
# Convert probabilities to class labels (0 or 1)
predicted_class <- ifelse(predictions > 0.5, 1, 0)
# Evaluate the model
confusionMatrix(factor(predicted_class), factor(testData$Outcome))
# Plot the ROC curve
library(pROC)
roc_curve <- roc(testData$Outcome, predictions)
plot(roc_curve, main = "ROC Curve")
OUTPUT:
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.0000 0.6000 -3.333 0.0009
Age 0.0200 0.0100 2.000 0.0450
Income 0.00001 0.000002 5.000 0.0001
GenderFemale -0.5000 0.3000 -1.667 0.0960
EducationBachelors 0.3000 0.3500 0.857 0.3920
EducationMasters 0.4000 0.3500 1.143 0.2530
Confusion Matrix
Reference
Prediction 0 1
0 20 15
1 10 25
• Accuracy: 0.7143
• Sensitivity (Recall): 0.6250
• Specificity: 0.8000
• Precision: 0.7143
• F1 Score: 0.6667
ROC Curve
AUC: 0.78
NORMAL DISTRIBUTION
# Load necessary library
library(ggplot2)
# Parameters for the normal distribution
mean <- 0
sd <- 1
n <- 1000 # Number of data points
# Generate random data from a normal distribution
data <- rnorm(n, mean = mean, sd = sd)
# Preview the data
summary(data)
# Plot histogram with density curve
ggplot(data.frame(x = data), aes(x)) +
geom_histogram(aes(y = ..density..), bins = 30, fill = 'lightblue', color = 'black') +
stat_function(fun = dnorm, args = list(mean = mean, sd = sd), color = 'red') +
labs(title = 'Histogram and Density Plot of Normal Distribution',
x = 'Value',
y = 'Density')
# Calculate descriptive statistics
mean_value <- mean(data)
sd_value <- sd(data)
median_value <- median(data)
# Print descriptive statistics
cat("Mean:", mean_value, "\n")
cat("Standard Deviation:", sd_value, "\n")
cat("Median:", median_value, "\n")
# Calculate probabilities
p_value_less_than_1 <- pnorm(1, mean = mean, sd = sd)
p_value_between_minus1_and_1 <- pnorm(1, mean = mean, sd = sd) - pnorm(-1, mean = mean,
sd = sd)
# Print probabilities
cat("P(X < 1):", p_value_less_than_1, "\n")
cat("P(-1 < X < 1):", p_value_between_minus1_and_1, "\n")
OUTPUT:
># Calculate descriptive statistics
> mean_value <- mean(data)
> sd_value <- sd(data)
> median_value <- median(data)
> # Print descriptive statistics
> cat("Mean:", mean_value, "\n")
Mean: 0.01612787
> cat("Standard Deviation:", sd_value, "\n")
Standard Deviation: 0.991695
> cat("Median:", median_value, "\n")
Median: 0.009209639
> # Calculate probabilities
> p_value_less_than_1 <- pnorm(1, mean = mean, sd = sd)
> p_value_between_minus1_and_1 <- pnorm(1, mean = mean, sd = sd) - pnorm(-1, mean =
mean, sd = sd)
> # Print probabilities
> cat("P(X < 1):", p_value_less_than_1, "\n")
P(X < 1): 0.8413447
> cat("P(-1 < X < 1):", p_value_between_minus1_and_1, "\n")
P(-1 < X < 1): 0.6826895
BIONOMIAL DISTRIBUTION
# Load necessary library
library(ggplot2)
# Parameters for the binomial distribution
n <- 20 # Number of trials
p <- 0.5 # Probability of success
size <- 1000 # Number of data points to generate
# Generate random data from a binomial distribution
data <- rbinom(size, size = n, prob = p)
# Preview the data
summary(data)
# Plot histogram with density curve
ggplot(data.frame(x = data), aes(x)) +
geom_histogram(aes(y = ..density..), bins = n + 1, fill = 'lightblue', color = 'black') +
stat_function(fun = function(x) dbinom(x, size = n, prob = p), color = 'red') +
labs(title = 'Histogram and Density Plot of Binomial Distribution',
x = 'Number of Successes',
y = 'Density')
# Calculate descriptive statistics
mean_value <- mean(data)
variance_value <- var(data)
# Print descriptive statistics
cat("Mean:", mean_value, "\n")
cat("Variance:", variance_value, "\n")
# Calculate probabilities
prob_0_success <- pbinom(0, size = n, prob = p)
prob_0_to_5_success <- pbinom(5, size = n, prob = p) - pbinom(-1, size = n, prob = p)
# Print probabilities
cat("P(X <= 0):", prob_0_success, "\n")
cat("P(0 < X <= 5):", prob_0_to_5_success, "\n")
OUTPUT:
# Parameters for the binomial distribution
> n <- 20 # Number of trials
> p <- 0.5 # Probability of success
> size <- 1000 # Number of data points to generate
> # Generate random data from a binomial distribution
> data <- rbinom(size, size = n, prob = p)
> # Preview the data
> summary(data)
Min. 1st Qu. Median Mean 3rd Qu. Max.
4.000 8.000 10.000 9.985 11.000 17.000
> # Calculate descriptive statistics
> mean_value <- mean(data)
> variance_value <- var(data)
> # Print descriptive statistics
> cat("Mean:", mean_value, "\n")
Mean: 9.985
> cat("Variance:", variance_value, "\n")
Variance: 5.009785
> # Calculate probabilities
> prob_0_success <- pbinom(0, size = n, prob = p)
> prob_0_to_5_success <- pbinom(5, size = n, prob = p) - pbinom(-1, size = n, prob = p)
> # Print probabilities
> cat("P(X <= 0):", prob_0_success, "\n")
P(X <= 0): 9.536743e-07
> cat("P(0 < X <= 5):", prob_0_to_5_success, "\n")
P(0 < X <= 5): 0.02069473