2016年12月4日 星期日

R_training_2016_1202_第三次上課

#################################################
# 第一章
# 描述性統計方法
#################################################
##  上課講義: biostat.tmu.edu.tw/R/20161202/#1
##  R圖形介面  install.packages("Rcmdr"), 只能用64bit 來執行!
## 讀取資料檔 ##
setwd("D:/R_work/")
babies = read.csv("babies.csv")
babies$parity = as.factor(babies$parity)
babies$smoke = as.factor(babies$smoke)

## 統計表 ##
t_smoke = table(babies$smoke, useNA="ifany")  # 次數 分配表
prop.table(t_smoke)  # 相對次數

## 集中量數 ##
mean(babies$bwt)  # 平均數
median(babies$gestation, na.rm=TRUE)  # 中位數
t_age = table(babies$age)
names(t_age[t_age %in% max(t_age)])  # 眾數

## 離勢量數 ##
var(babies$height, na.rm=TRUE)  # 變異數
var(babies[-c(3,7)], use="complete.obs")  #一起比較許多的 行
sd(babies$weight, na.rm=TRUE)  # 標準差
sd(babies$bwt, na.rm=TRUE) / mean(babies$bwt, na.rm=TRUE)  # 變異係數, 用來做比較時用,較不受資料本身影響
range(babies$gestation, na.rm=TRUE)  # 全距
min(babies$gestation, na.rm=TRUE)  # 最小值
max(babies$gestation, na.rm=TRUE)  # 最大值
IQR(babies$age, na.rm=TRUE)  # 內四分位距, 就是Q3-Q1, Q2 就是median
quantile(babies$age, na.rm=TRUE)  # 分位數


#################################################
# 第二章
# 基本繪圖功能
#################################################

## 高階繪圖函數 ##
plot(babies$bwt)
plot(babies[c("age", "height", "weight")])  ## 數值資料就是 scatter plote
plot(as.factor(babies$smoke))  ## factor 就變柱狀圖
plot(as.factor(babies$smoke), babies$bwt)  ## 有數值, 有類別 就盒?圖
plot(iris[,1], type="l")  # p, l, b, c, o, h, s, S, n
hist(babies$bwt, nclass=10, col="lightblue")  # 直方圖, !適合用連續變數資料
barplot(table(babies$smoke), col=2:3)  # 長條圖, 適合離散資料..要先做table!!
counts = table(babies$smoke, babies$parity)
barplot(counts, beside=TRUE, xlab="parity", legend=rownames(counts))
boxplot(iris[1:4])  # 盒鬚圖
boxplot(Sepal.Length ~ Species, data=iris, col=2:4, horizontal=TRUE)  # ~ formula, !!
pie(table(iris$Species))  # 圓餅圖
library(plotrix)
pie3D(table(iris$Species), theta=1, explode=0.1)

## 低階繪圖函數 ##
hist(iris[,2])
x = c(2.5, 3, 3.5)
y = c(18, 7, 12)
label = c("A", "B", "C")
points(x, y, pch=1:3)
lines(x, y, col=4)
text(x, y+2, labels=label)
legend(4, 35, legend=label, pch=1:3, lty=1, col=4)


#################################################
# 第三章
# 相關係數
#################################################

## 相關係數 ##
use = "pairwise.complete.obs"
cor(babies[1:2], use=use, method="pearson")  # 皮爾生相關係數, 非常容易受extreme 影響
cor(babies$bwt, babies$gestation, use=use)
cor(babies[1:2], use=use, method="spearman")  # 斯皮爾曼等級相關係數
cor(babies$bwt, babies$gestation, use=use, method="spearman")


#################################################
# 第四章
# 迴歸模式
#################################################

## 簡單線性迴歸分析 ##
fit1 = lm(bwt ~ gestation, data=babies)
summary(fit1)
plot(babies$gestation, babies$bwt)
abline(fit1$coefficients, col=2)

## 多元線性迴歸分析 ##
fit2 = lm(bwt ~ ., data=na.exclude(babies))  # 為了後續使用 step 函數,先行排除遺失值資料
summary(fit2)
s_fit2 = step(fit2, direction="backward") #另一種方法幫你建議那些變數不適合!不建議用"forward"
summary(s_fit2)
===========================================




########################################################
##          R 軟體系列課程 - R 在統計方法上的應用
##          課堂練習題參考答案
##          2016/12/02
########################################################


######################
##  描述性統計方法  ##
######################

## 繪製babies資料中parity變數的次數分配和相對次數分配表 ##
t_parity = table(babies$parity)
prop.table(t_parity)

## 以smoke為分組變數,計算babies資料中height, weight兩變數的內四分位距(IQR) ##
sapply(babies[c("height","weight")], tapply, babies$smoke, IQR, na.rm=TRUE)  #sapply 就是對某幾個變數, 做tapply... 後面試tapply的變數..
tapply(babies$height, babies$smoke, IQR, na.rm=TRUE)  #先想一下, 單純的情形, tapply做的事情就" 依據抹一個變數, 對另一個變數, 做function...

####################
##  基本繪圖功能  ##
####################

## 試繪製出以下圖形 ##
n = nrow(iris)
plot(1:n, iris$Sepal.Length, type="o", ylim=c(0,max(iris$Sepal.Length)))  # 先將 y 軸範圍空出來
points(1:n, iris$Sepal.Width, type="p", col=2)
lines(1:n, iris$Petal.Length, type="b", col=3)
legend(100, 1.5, legend=c("Sepal.Length","Sepal.Width","Petal.Length"), pch=1, col=1:3)


################
##  相關係數  ##
################

## 分別使用皮爾生和斯皮爾曼方法計算babies資料中gestation, height, weight變數的兩兩相關係數,並解釋其意義 ##
cor(babies[c("gestation", "height", "weight")], use="pairwise.complete.obs")  # 可試試使用其他 use 值的輸出有何不同
cor(babies[c("gestation", "height", "weight")], use="pairwise.complete.obs", method="spearman")


################
##  迴歸模式  ##
################

## 以babies資料建立一個線性迴歸模式,其中bwt為依變數、其餘變數為自變數,並加入gestation與age的交互作用項。嘗試解釋模型係數的意義 ##
fit = lm(bwt ~ . + gestation:age, data=babies)
summary(fit)


沒有留言: