write code in R to do:
-correlation coefficients or chi-square test of independence (any);
-ANOVA OR T-test;
-Linear regression analysis OR Logistic regression analysis;
-Exploratory factor analysis OR Principal component analysis OR Cluster analysis (k-means).
Correlation coefficients:
# create two variables
x <- c(1, 2, 3, 4, 5)
y <- c(5, 4, 3, 2, 1)
# calculate the correlation coefficient
cor(x, y)
Chi-square test of independence:
# create a contingency table
data <- matrix(c(10, 20, 30, 40), nrow = 2)
colnames(data) <- c("Group A", "Group B")
rownames(data) <- c("Male", "Female")
# perform chi-square test
chisq.test(data)
One-way ANOVA:
# create three groups of data
a <- c(20, 25, 30, 35, 40)
b <- c(15, 20, 25, 30, 35)
c <- c(10, 15, 20, 25, 30)
# perform ANOVA
anova(lm(c(a, b, c) ~ rep(c("a", "b", "c"), each = 5)))
Linear regression analysis:
# create two variables
x <- c(1, 2, 3, 4, 5)
y <- c(5, 4, 3, 2, 1)
# perform linear regression
lm(y ~ x)
Logistic regression analysis:
# create binary response variable and predictor
response <- c("Yes", "No", "No", "Yes", "No", "No", "Yes", "Yes")
predictor <- c(1, 2, 3, 4, 5, 6, 7, 8)
# perform logistic regression
glm(response ~ predictor, family = binomial)
Exploratory factor analysis:
# create a data set
data <- data.frame(x = rnorm(50), y = rnorm(50), z = rnorm(50))
# perform exploratory factor analysis
library("psych")
fa(data, nfactors = 2)
Principal component analysis:
# create a data set
data <- data.frame(x = rnorm(50), y = rnorm(50), z = rnorm(50))
# perform principal component analysis
prcomp(data)
Cluster analysis (k-means):
# create a data set
data <- data.frame(x = rnorm(50), y = rnorm(50))
# perform cluster analysis
kmeans(data, centers = 2)