R語言分析實戰——考研人數的影響因素
阿新 • • 發佈:2020-12-30
原始資料
程式碼
data = read.csv('原始資料.csv',header = T) # 畫圖 attach(data) par(mfrow=c(2,2)) plot(年份, 報名人數, pch = 15, lty = 1, col = "red", xlab = "年份", ylab = "報名人數") abline(lsfit(年份,報名人數)) plot(年份, 錄取人數, type = "b", pch = 16, lty = 2, col = "blue", xlab = "年份", ylab = "錄取人數") plot(年份, 畢業生人數, type = "b", pch = 17, lty = 3, col = "orange", xlab = "年份", ylab = "畢業生人數") plot(年份, 就業率, type = "b", pch = 15, lty = 1, col = "black", xlab = "年份", ylab = "就業率") par(mfrow=c(1,1)) detach(data) # 相關性分析 #library(psych) #corr.test(data[,c(2,3,4,5)]) # 上面是相關係數,下面是檢測值 cov(data[,c(2,3,4,5)]) # 檢視相關係數 # 報名人數與錄取人數的相關性 cor.test(data[,2],data[,3]) # 報名人數與畢業生人數的相關性 cor.test(data[,2],data[,4]) # 報名人數與就業率的相關性 cor.test(data[,2],data[,5]) # 多元線性迴歸 fit = lm(報名人數~錄取人數+畢業生人數+就業率) summary(fit) # 檢視擬合效果 options(digits = 4) # 保留四位數 coef(fit) # 檢視係數 # 畫迴歸後對照圖形 plot(年份, 報名人數, type = "b", pch = 15, lty = 1, col = "black", xlab = "年份", ylab = "報名人數",ylim = c(140,300)) lines(年份,迴歸資料,type = "b",pch = 16,lty=2,col = "red") legend("topleft",c('源資料','迴歸資料'), lty = c(1,2), pch = c(15,16),col=c('black','red'))