R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
The Poisson distribution is a probability distribution that represents the number of events in a fixed interval of time or space. It is commonly used in various fields such as biology, finance, and traffic engineering.
R provides several functions related to the Poisson distribution:
dpois
: Gives the probability mass function.ppois
: Gives the cumulative distribution function.qpois
: Gives the quantile function.rpois
: Generates random numbers.Let's go through each function with examples:
dpois
: Probability Mass Function (PMF)This function gives the probability of observing a specific number of events.
# Probability of observing 3 events when lambda (average rate of events) is 5 dpois(3, lambda=5)
ppois
: Cumulative Distribution Function (CDF)This function returns the probability of observing up to a specific number of events.
# Probability of observing 3 or fewer events when lambda is 5 ppois(3, lambda=5)
qpois
: Quantile FunctionGiven a probability, this function returns the number of events such that there's that probability of observing up to that many events.
# What's the number of events such that there's a 25% chance of observing that many or fewer when lambda is 5? qpois(0.25, lambda=5)
rpois
: Generating Random NumbersThis function generates random numbers from a Poisson distribution.
# Generate 10 random numbers from a Poisson distribution with lambda = 5 rpois(10, lambda=5)
A good way to understand the Poisson distribution is by visualizing it. Here's a simple plot using the dpois
function:
lambda <- 5 x <- 0:15 # considering values from 0 to 15 for our visualization # Calculate probabilities for each x probabilities <- dpois(x, lambda) # Plot plot(x, probabilities, type="h", main="Poisson Distribution PMF with lambda=5", xlab="Number of Events", ylab="Probability", lwd=2, col="blue") points(x, probabilities, pch=16, col="red")
This plot provides a clear visualization of how the probability changes with the number of events for a given lambda in a Poisson distribution.
Remember, the Poisson distribution assumes that events are rare, independent, and occur at a constant rate in a given interval of time or space. If these assumptions are not met, then the Poisson distribution might not be appropriate.
Generating Poisson random numbers in R:
Overview: Introduce the concept of Poisson random numbers and provide an example.
Code:
# Generating Poisson random numbers in R lambda <- 3 # Poisson parameter (average rate) poisson_random_numbers <- rpois(100, lambda) # Displaying the generated numbers print("Generated Poisson Random Numbers:") print(poisson_random_numbers)
R dpois function examples:
Overview: Illustrate the usage of the dpois()
function for Poisson probability mass.
Code:
# R dpois function examples lambda <- 3 # Poisson parameter (average rate) x_values <- 0:10 # Probability mass function values pmf_values <- dpois(x_values, lambda) # Displaying the results print("Poisson PMF Values:") print(paste("X:", x_values)) print(paste("P(X):", pmf_values))
Fitting Poisson regression models in R:
Overview: Introduce the concept of Poisson regression and demonstrate model fitting.
Code: Provide an example using functions like glm()
.
# Fitting Poisson regression models in R data <- data.frame(counts = c(2, 5, 3, 8, 6), predictors = c(1, 2, 3, 4, 5)) # Poisson regression model poisson_model <- glm(counts ~ predictors, data = data, family = poisson) # Displaying model summary summary(poisson_model)
Poisson test in R for count data:
Overview: Explain the use of statistical tests for Poisson-distributed count data.
Code: Showcase a test using functions like poisson.test()
.
# Poisson test in R for count data observed_counts <- c(8, 12, 10, 15, 9) expected_counts <- c(10, 10, 10, 10, 10) # Poisson test poisson_test_result <- poisson.test(observed_counts, T = sum(expected_counts)) # Displaying the test result print("Poisson Test Result:") print(poisson_test_result)
Poisson distribution plots in R:
Overview: Visualize the Poisson distribution using plots.
Code:
# Poisson distribution plots in R lambda <- 3 # Poisson parameter (average rate) x_values <- 0:10 # Probability mass function values pmf_values <- dpois(x_values, lambda) # Plotting the Poisson distribution plot(x_values, pmf_values, type = "h", lwd = 2, col = "blue", main = "Poisson Distribution", xlab = "X", ylab = "P(X)")
Using ppois function for cumulative Poisson probabilities in R:
Overview: Demonstrate the use of ppois()
for cumulative probabilities.
Code:
# Using ppois function for cumulative Poisson probabilities in R lambda <- 3 # Poisson parameter (average rate) x_values <- 0:10 # Cumulative probabilities cumulative_probs <- ppois(x_values, lambda) # Displaying the results print("Cumulative Poisson Probabilities:") print(paste("X:", x_values)) print(paste("P(X <= x):", cumulative_probs))
Poisson regression analysis in R:
Overview: Conduct a comprehensive Poisson regression analysis.
Code: Extend the Poisson regression example to include predictions and diagnostics.
# Poisson regression analysis in R data <- data.frame(counts = c(2, 5, 3, 8, 6), predictors = c(1, 2, 3, 4, 5)) # Poisson regression model poisson_model <- glm(counts ~ predictors, data = data, family = poisson) # Predictions predicted_counts <- predict(poisson_model, newdata = data, type = "response") # Displaying results print("Poisson Regression Analysis:") print("Model Summary:") print(summary(poisson_model)) print("Predicted Counts:") print(predicted_counts)
R code for simulating Poisson processes:
Overview: Simulate Poisson processes using the rpois()
function.
Code:
# R code for simulating Poisson processes lambda <- 3 # Poisson parameter (average rate) simulation_size <- 100 # Simulating Poisson process simulated_counts <- rpois(simulation_size, lambda) # Displaying the simulation results print("Simulated Poisson Process:") print(simulated_counts)