12. This problem involves simple linear regression without an intercept.

(a) Recall that the coefficient estimate ˆ β for the linear regression of Y onto X without an intercept is given by (3.38). Under what circumstance is the coefficient estimate for the regression of X onto Y the same as the coefficient estimate for the regression of Y onto X?

#If the variances of X and Y are the same and the means of both variables are zero, the regression coefficients will be the same.

(b) Generate an example in R with n = 100 observations in which the coefficient estimate for the regression of X onto Y is different from the coefficient estimate for the regression of Y onto X.

# Setting the seed for reproducibility

set.seed(54) 

# Generate X and Y with different variances

X <- rnorm(100, mean = 5, sd = 2)  # Sd = 2
Y <- 3 * X + rnorm(100, mean = 0, sd = 5) # Larger variance for Y

# Regression of Y onto X

beta_y_on_x <- sum(X * Y) / sum(X^2)

# Regression of X onto Y

beta_x_on_y <- sum(X * Y) / sum(Y^2)

# show results

cat("β (Y onto X):", beta_y_on_x, "\n")
## β (Y onto X): 3.077613
cat("β (X onto Y):", beta_x_on_y, "\n")
## β (X onto Y): 0.2945874

# β (Y onto X): 3.077613 # β (X onto Y): 0.2945874
# The two coefficient are not equal

(c) Generate an example in R with n = 100 observations in which the coefficient estimate for the regression of X onto Y is the same as the coefficient estimate for the regression of Y onto X.

set.seed(54)

# Generate X values

X <- rnorm(100)

# Create Y to have the same variance as X

Y <- X * sqrt(sum(X^2) / sum(X^2))  # This ensures sum(X^2) = sum(Y^2)

# Regression of Y onto X

beta_y_on_x <- sum(X * Y) / sum(X^2)

# Regression of X onto Y

beta_x_on_y <- sum(X * Y) / sum(Y^2)

# Print results

cat("β (Y onto X):", beta_y_on_x, "\n")
## β (Y onto X): 1
cat("β (X onto Y):", beta_x_on_y, "\n")
## β (X onto Y): 1

# Both regression coefficient are equal to 1 # β (Y onto X): 1 # β (X onto Y): 1