注:練習(xí)一 ~ 四過于簡單揽浙,略
練習(xí)五 - 數(shù)據(jù)讀取及t.test檢驗(yàn)
本練習(xí)主要做的是一個(gè)t.test,有兩種導(dǎo)入數(shù)據(jù)方式
(1)當(dāng)數(shù)據(jù)較少時(shí)可以直接賦值:
weight <- c(60,72,57,90,95,72)
height <- c(1.75,1.8,1.65,1.9,1.74,1.91)
然后計(jì)算它們的比率并查看:
ratio <- weight/height^2
head(ratio)
#[1] 19.59184 22.22222 20.93664 24.93075 31.37799 19.73630
t.test計(jì)算結(jié)果展示:
t.test(ratio)
結(jié)果如下:
One Sample t-test
data: ratio
t = 12.611, df = 5, p-value = 5.569e-05
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
18.41734 27.84791
sample estimates:
mean of x
23.13262
.
(2)當(dāng)數(shù)據(jù)較多時(shí)肩杈,就需要從外部讀取進(jìn)入數(shù)據(jù)文檔了虐秋,一般用“txt”格式文檔,導(dǎo)入文檔需要注意的一點(diǎn)奴紧,也是前提:設(shè)置環(huán)境授帕,這里需要用到函數(shù)setwd()
setwd("F:/XJAU//work/Advanced biostatistics-work/")
導(dǎo)入數(shù)據(jù)(數(shù)據(jù)自己在PPT上扣)
t.test.data <- read.table("t.test.txt",header = T)
如步驟(1)計(jì)算
attach(t.test.data)
ratio <- t.test.data$weight/t.test.data$height^2
t.test(ratio)
.
練習(xí)六 - 方差分析及箱線圖繪制
導(dǎo)入數(shù)據(jù)
bac <- read.table("anova.data.txt",header = T)
將bac數(shù)據(jù)框中的type轉(zhuǎn)換為因子(factor)
bac$type <- as.factor(bac$type)
單因素方差分析
ba.an <- aov(lm(day ~ type,data = bac))
#head(ba.an)
summary(ba.an)
繪制箱線圖
boxplot(day ~ type,data = bac,col = "red")
如圖
三種菌型對(duì)小鼠存活天數(shù)的影響
·
練習(xí)七 - 因子生成
注:該題直接照著PPT敲就行了同木,但是我敲好了就粘貼進(jìn)來了
numeric <- c(100,200,400,600,800)
factor.numeric <- as.factor(numeric)
factor.numeric
.
練習(xí)八 - 條件篩選
1.創(chuàng)建一個(gè)2到50的向量
vectorl <- seq(from = 2,to = 50, by = 2)
head(vectorl)
2.選取第20個(gè)元素
vectorl[20]
3.選取第10,15,20個(gè)元素
vectorl[c(10,15,20)]
4.選取第10到20的元素
vectorl[10:20]
5.選取大于40的元素
vectorl[vectorl>40]
.
練習(xí)九 - 了解工作路徑
查看當(dāng)前環(huán)境
getwd()
將工作路徑設(shè)置為:"d:/data/"
setwd("d:/data/")
.
練習(xí)十 - 運(yùn)行腳本
注:此題較復(fù)雜豪墅,不宜展示泉手,僅注釋
此腳本為rate和impurity之間的回歸分析
賦值數(shù)據(jù)
rate <- c(20,22,24,26,28,30,32,34,36,38,40,42)
impurity <- c(8.4,9.5,11.8,10.4,13.3,14.8,13.2,14.7,16.4,16.5,18.9,18.5)
繪制散點(diǎn)圖
plot(impurity ~ rate)
回歸分析
reg <- lm(impurity ~ rate)
abline(reg,col = "red")
summary(reg)
繪圖如下:
回歸分析
.
練習(xí)十一 - 繪圖練習(xí)
1. 直接粘貼練習(xí)十的腳本
2.回歸分析及繪制散點(diǎn)圖
plot(impurity ~ rate)
reg <- lm(impurity ~ rate)
添加回歸直線
abline(reg,col = "red")
summary(reg)
結(jié)果如上圖
.
練習(xí)十二 - 編寫函數(shù)
編寫一個(gè)可以計(jì)算兩者平方和的臨時(shí)函數(shù)
sqtest <- function(x,y)
{
z1 = x^2;
z2 = y^2;
z3 = z1 + z2;
z3
}
計(jì)算平方和
sqtest(3,4)
結(jié)果等于25
.
花有重開時(shí),人無再少年