WEEK2
- TANG HAORAN

- 2024年3月12日
- 讀畢需時 1 分鐘
PEARSON
# 加载所需的包
library(ggplot2)
# 假设你的数据已经存储在CSV文件中,变量名分别为Var1和Var2
# 读取CSV文件
data <- read.csv("C:/Users/11030/Desktop/HKtestno0.csv")
# 执行Pearson相关性分析
result <- cor.test(data$garbagebin, data$restaurant, method = "pearson")
# 打印相关系数和p值
print(paste("Pearson correlation coefficient:", result$estimate))
print(paste("P-value:", result$p.value))
# 判断相关性是否显著
if (result$p.value < 0.05) {
print("There is a statistically significant correlation between Var1 and Var2.")
} else {
print("There is no statistically significant correlation between Var1 and Var2.")
}
# 使用ggplot2创建散点图并添加线性回归拟合线
ggplot(data, aes(x = garbagebin, y = restaurant)) +
geom_point() + # 添加散点图层
geom_smooth(method = "lm", se = FALSE, color = "blue") + # 添加线性回归拟合线,不包含置信区间
theme_minimal() + # 使用简洁主题
labs(x = "garbagebin", y = "restaurant", title = "Pearson Correlation with Linear Regression Line") # 添加标签和标题

留言