top of page

WEEK2

  • 作家相片: TANG HAORAN
    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") # 添加标签和标题


最新文章

查看全部
WEEK13

Week 13: Wrapping up my journey of learning R programming. Consolidated my knowledge and practiced with various exercises. Feeling...

 
 
 
WEEK12

Week 12: Made significant progress in R programming. Explored advanced topics like data mining and machine learning. Excited to dive...

 
 
 

留言


Silver Team

©2024 Silver Team 版權所有。透過 Wix.com 製作的理想網站

bottom of page