高级生物统计学练习作业

注:练习一 ~ 四过于简单,略

练习五 - 数据读取及t.test检验

本练习主要做的是一个t.test,有两种导入数据方式

(1)当数据较少时可以直接赋值:

weight <- c(60,72,57,90,95,72)
height <- c(1.75,1.8,1.65,1.9,1.74,1.91)

然后计算它们的比率并查看:

ratio <- weight/height^2
head(ratio)
#[1] 19.59184 22.22222 20.93664 24.93075 31.37799 19.73630

t.test计算结果展示:

t.test(ratio)

结果如下:

    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)当数据较多时,就需要从外部读取进入数据文档了,一般用“txt”格式文档,导入文档需要注意的一点,也是前提:设置环境,这里需要用到函数setwd()

setwd("F:/XJAU//work/Advanced biostatistics-work/")

导入数据(数据自己在PPT上扣)

t.test.data <- read.table("t.test.txt",header = T)

如步骤(1)计算

attach(t.test.data)
ratio <- t.test.data$weight/t.test.data$height^2
t.test(ratio)

.

练习六 - 方差分析及箱线图绘制

导入数据

bac <- read.table("anova.data.txt",header = T)

将bac数据框中的type转换为因子(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")

如图

三种菌型对小鼠存活天数的影响

·

练习七 - 因子生成

注:该题直接照着PPT敲就行了,但是我敲好了就粘贴进来了

numeric <- c(100,200,400,600,800)
factor.numeric <- as.factor(numeric)
factor.numeric

.

练习八 - 条件筛选

1.创建一个2到50的向量

vectorl <- seq(from = 2,to = 50, by = 2)
head(vectorl)

2.选取第20个元素

vectorl[20]

3.选取第10,15,20个元素

vectorl[c(10,15,20)]

4.选取第10到20的元素

vectorl[10:20]

5.选取大于40的元素

vectorl[vectorl>40]

.

练习九 - 了解工作路径

查看当前环境

getwd()

将工作路径设置为:"d:/data/"

setwd("d:/data/")

.

练习十 - 运行脚本

注:此题较复杂,不宜展示,仅注释

此脚本为rate和impurity之间的回归分析

赋值数据

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)

绘制散点图

plot(impurity ~ rate)

回归分析

reg <- lm(impurity ~ rate)
abline(reg,col = "red")
summary(reg)

绘图如下:

回归分析

.

练习十一 - 绘图练习

1. 直接粘贴练习十的脚本

2.回归分析及绘制散点图

plot(impurity ~ rate)
reg <- lm(impurity ~ rate)

添加回归直线

abline(reg,col = "red")
summary(reg) 

结果如上图

.

练习十二 - 编写函数

编写一个可以计算两者平方和的临时函数

sqtest <- function(x,y)
  {
   z1 = x^2;
   z2 = y^2;
   z3 = z1 + z2;
   z3
}

计算平方和

sqtest(3,4)

结果等于25

.

花有重开时,人无再少年

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。