0% found this document useful (0 votes)
2 views

code

The document outlines a series of statistical analyses using R, including calculations of horsepower statistics from the mtcars dataset, a linear relationship analysis between height and weight, Poisson distribution probabilities, and normal distribution calculations. It includes generating plots for histograms, regression lines, and probability distributions. Each task is accompanied by code snippets that perform the required calculations and visualizations.

Uploaded by

matlabdec12
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code

The document outlines a series of statistical analyses using R, including calculations of horsepower statistics from the mtcars dataset, a linear relationship analysis between height and weight, Poisson distribution probabilities, and normal distribution calculations. It includes generating plots for histograms, regression lines, and probability distributions. Each task is accompanied by code snippets that perform the required calculations and visualizations.

Uploaded by

matlabdec12
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Task 1: mtcars dataset analysis

# Load dataset
data(mtcars)

# i) Calculate required statistics for hp


mean_hp <- mean(mtcars$hp)
median_hp <- median(mtcars$hp)
Q3_hp <- quantile(mtcars$hp, 0.75)
min_hp <- min(mtcars$hp)
max_hp <- max(mtcars$hp)

# Print results
print(paste("Mean HP:", mean_hp))
print(paste("Median HP:", median_hp))
print(paste("Third Quartile HP:", Q3_hp))
print(paste("Minimum HP:", min_hp))
print(paste("Maximum HP:", max_hp))

# ii) Plot histogram of hp


hist(mtcars$hp, main="Histogram of Horsepower", col="blue", xlab="Horsepower",
border="black")

# Task 2: Linear relationship between weight and height


# Generate sample data
set.seed(42)
height <- round(runif(10, 150, 190)) # Height in cm
weight <- round(runif(10, 50, 100)) # Weight in kg
data <- data.frame(Height = height, Weight = weight)

# Linear model
lm_model <- lm(Weight ~ Height, data = data)
print(summary(lm_model))

# Plot data with regression line


plot(data$Height, data$Weight, main="Height vs Weight", xlab="Height (cm)",
ylab="Weight (kg)", pch=16, col="red")
abline(lm_model, col="blue", lwd=2)

# Task 3: Poisson distribution calculations


lambda <- 16

# i) P(X ≤ 15)
prob_15_or_less <- ppois(15, lambda)
print(paste("P(X ≤ 15):", prob_15_or_less))

# ii) P(X = 6)
prob_exact_6 <- dpois(6, lambda)
print(paste("P(X = 6):", prob_exact_6))

# iii) P(X ≥ 15)


prob_15_or_more <- 1 - ppois(14, lambda)
print(paste("P(X ≥ 15):", prob_15_or_more))

# iv) Plot Poisson probabilities


x_vals <- 0:30
y_vals <- dpois(x_vals, lambda)
plot(x_vals, y_vals, type="h", main="Poisson Distribution (λ=16)",
xlab="Occurrences", ylab="Probability", col="blue", lwd=2)
points(x_vals, y_vals, pch=16, col="red")
# Task 4: Normal distribution calculations
mu <- 20 # Mean
sigma <- 5 # Standard deviation

# i) P(X < 15)


prob_less_15 <- pnorm(15, mean = mu, sd = sigma)
print(paste("P(X < 15):", prob_less_15))

# ii) P(X > 25)


prob_more_25 <- 1 - pnorm(25, mean = mu, sd = sigma)
print(paste("P(X > 25):", prob_more_25))

# iii) P(15 ≤ X ≤ 25)


prob_between_15_25 <- pnorm(25, mean = mu, sd = sigma) - pnorm(15, mean = mu, sd =
sigma)
print(paste("P(15 ≤ X ≤ 25):", prob_between_15_25))

# iv) Plot normal curve


x_vals <- seq(0, 40, length=100)
y_vals <- dnorm(x_vals, mean=mu, sd=sigma)
plot(x_vals, y_vals, type="l", main="Normal Distribution (Mean=20, SD=5)",
xlab="Time (minutes)", ylab="Density", col="blue", lwd=2)
abline(v=c(15, 25), col="red", lwd=2, lty=2)

You might also like