#Write a program to create an any application of Linear Regression in multivariate context for
predictive purpose.
print("Multivariate Analysis -PCA")
d <- data.frame(X1 = c(0, 1, 0, 0,2,3,1,0), X2 = c(0, 2, 2, 0,3,1,0,2))
pca <- prcomp(d,center = TRUE, scale. = TRUE)
# Print the summary of the PCA results
print(summary(pca))
set.seed(123)
n <- 10
p <- 3
new_data <- matrix(rnorm(n * p), nrow = n, ncol = p)
colnames(new_data) <- paste("Var", 1:p)
print("DATA")
print(new_data)
pca_result <- prcomp(new_data,center = TRUE, scale. = TRUE)
new_data_scaled <- scale(new_data, center = pca_result$center, scale = pca_result$scale)
new_pca_scores <- as.matrix(new_data_scaled) %*% pca_result$rotation
pca_scores=new_pca_scores
model <- lm(new_data ~ pca_scores[, 1] + pca_scores[, 2])
predictions <- predict(model, newdata = data.frame(PC1 = new_pca_scores[, 1],
PC2 = new_pca_scores[, 2]))
print("Predications")
print(max.col(predictions,ties.method = "random") )