This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

CORRELATION

# Generate age data
age <- c(25, 30, 35, 40, 45, 50, 55, 60, 65, 70)

# Generate corresponding height data
height <- c(160, 165, 170, 175, 180, 185, 190, 195, 200, 205)

# Calculate xy
age_height <- age * height

# Calculate the sum of xy
sum_age_height <- sum(age_height)

# Calculate n (number of data points)
n <- length(age)

# Calculate n * sum_xy
n_times_sum_age_height <- n * sum_age_height

# Calculate sum of age
sum_age <- sum(age)

# Calculate sum of height
sum_height <- sum(height)

# Calculate sum of age * sum of height
sum_age_times_sum_height <- sum_age * sum_height

# Calculate the numerator
num <- n * sum_age_height - sum_age * sum_height

# Calculate sq of age
age_sq <- age^2

# Calculate sum of sq of age
sum_age_sq <- sum(age_sq)

# Calculate n * sum of sq of age
n_times_sum_age_sq <- n * sum_age_sq

# Calculate sq of sum age
sq_sum_age <- sum_age^2

# Calculate sq of height
height_sq <- height^2

# Calculate sum of sq of height
sum_height_sq <- sum(height_sq)

# Calculate n * sum of sq of height
n_times_sum_height_sq <- n * sum_height_sq

# Calculate sq of sum height
sq_sum_height <- sum_height^2

# Calculate the denominator
deno <- ((n * sum_age_sq) - sq_sum_age) * ((n * sum_height_sq) - sq_sum_height)

# Calculate the square root of the denominator
sq_root_deno <- deno^0.5

# Calculate the correlation coefficient (R)
r <- num / sq_root_deno

# Output the correlation coefficient
r
## [1] 1

REGRESSION

# Given data
age <- c(25, 30, 35, 40, 45, 50, 55, 60, 65, 70)
height <- c(160, 165, 170, 175, 180, 185, 190, 195, 200, 205)

# Sum of age
sum_age <- sum(age)

# Number of data points (length of age vector)
n <- length(age)

# Sum of height
sum_height <- sum(height)

# Product of age and height (xy)
age_height <- age * height

# Sum of xy
sum_age_height <- sum(age_height)

# Squared values of age
sq_age <- age^2

# Sum of squared values of age
sum_sq_age <- sum(sq_age)

# Numerator of the slope (b)
num <- (sum_age_height * n) - (sum_age * sum_height)

# Denominator of the slope (b)
deno <- (n * sum_sq_age) - (sum_age)^2

# Calculate the slope (b)
b <- num / deno
b
## [1] 1
# Calculate the intercept (a)
mean_age <- sum_age / n
mean_height <- sum_height / n
a <- mean_height - b * mean_age

# Output the slope and intercept
print(paste("Slope (b):", b))
## [1] "Slope (b): 1"
print(paste("Intercept (a):", a))
## [1] "Intercept (a): 135"