题目链接:https://mp.weixin.qq.com/s/0dGPaHh1VftCdZ4xOaE_Wg
基础概念
Q1
data(iris)
- 前4列定量,最后1列定性。
Q2
#以第1列Sepal.Length为例
mode <- function(x) {
uniq <- unique(x)
tab <- tabulate(match(x,uniq))
uniq[tab == max(tab)]
}
apply(iris[,1:4],2,mode)#众数
apply(iris[,1:4],2,fivenum)#分位数
apply(iris[,1:4],2,mean)#平均数
Q3
table(iris$Species)
Q4
apply(iris[,1:4],2,sd)#标准差
apply(iris[,1:4],2,var)#方差
Q5
?cor
cor(iris$Sepal.Length,iris$Sepal.Width, method = "pearson")
cor(iris$Sepal.Length,iris$Sepal.Width, method = "kendall")
cor(iris$Sepal.Length,iris$Sepal.Width, method = "spearman")
Q6
iris_scale=t(scale(t(iris[,1:4])))
apply(iris_scale,2,mean)#平均数
apply(iris_scale,2,sd)#标准差
Q7
cor(iris_scale[,1],iris_scale[,2], method = "pearson")
cor(iris_scale[,1],iris_scale[,2], method = "kendall")
cor(iris_scale[,1],iris_scale[,2], method = "spearman")
Q8
- 做到这里发现上当了,前面的计算居然要重复好几次,那写个函数吧!
do_it <- function(data){
mode <- apply(data[,1:4],2,mode)#众数
fivenum <- apply(data[,1:4],2,fivenum)#分位数
mean <- apply(data[,1:4],2,mean)#平均数
frequence <- table(data$Species)
sd <- apply(data[,1:4],2,sd)#标准差
var <- apply(data[,1:4],2,var)#方差
cor <- cor(data[,1],data[,2])
data_scale=t(scale(t(data[,1:4])))
mean_scale <- apply(data_scale,2,mean)#平均数
sd_scale <- apply(data_scale,2,sd)#标准差
cor_scale <- cor(data_scale[,1],data_scale[,2])
return(c(mean=mean, fivenum=fivenum, sd=sd, var=var, frequence=frequence,
mode=mode, cor=cor, mean_scale=mean_scale, sd_scale=sd_scale,
cor_scale=cor_scale))
}
iris_set <- iris[iris$Species=="setosa",]
iris_ver <- iris[iris$Species=="versicolor",]
iris_vir <- iris[iris$Species=="virginica",]
do_it(iris_set)
do_it(iris_ver)
do_it(iris_vir)
Q9
data(mtcars)
- 可以看到和刚才的数据结构有所不同,所以我们对
do_it
函数略作修改
do_it <- function(data){
mode <- apply(data,2,mode)#众数
fivenum <- apply(data,2,fivenum)#分位数
mean <- apply(data,2,mean)#平均数
frequence <- table(data$cyl)
sd <- apply(data,2,sd)#标准差
var <- apply(data,2,var)#方差
cor <- cor(data[,1],data[,2])
data_scale=t(scale(t(data)))
mean_scale <- apply(data_scale,2,mean)#平均数
sd_scale <- apply(data_scale,2,sd)#标准差
cor_scale <- cor(data_scale[,1],data_scale[,2])
return(c(mean=mean, fivenum=fivenum, sd=sd, var=var, frequence=frequence,
mode=mode, cor=cor, mean_scale=mean_scale, sd_scale=sd_scale,
cor_scale=cor_scale))
}
do_it(mtcars)
Q10
rm(list = ls())
options(stringsAsFactors = F)
library(airway)
data(airway)
RNAseq_expr=assay(airway)
colData(airway)
RNAseq_gl=colData(airway)[,3]
table(RNAseq_gl)
M <- cor(RNAseq_expr)
pheatmap::pheatmap(M)
表达矩阵相关
Q1
load("expr.Rdata")
tmp=log2(RNAseq_expr[,1]+1)
mean(tmp)
sd(tmp)
Q2
a=rnorm(length(tmp),mean = mean(tmp),sd = sd(tmp))
Q3
tmp=RNAseq_expr[RNAseq_expr[,1]>5,]
tmp=log2(tmp[,1]+1)
mean(tmp)
sd(tmp)
a=rnorm(length(tmp),mean = mean(tmp),sd = sd(tmp))
Q4
x=RNAseq_expr[,1]
x=x[x>5]
x=log2(x)
y=RNAseq_expr[,2]
y=y[y>5]
y=log2(y)
t.test(x,y)
Q5
t.test(RNAseq_expr[which.max(rowSums(RNAseq_expr)),]~RNAseq_gl)
Q6
t.test(RNAseq_expr[which.max(apply(RNAseq_expr,1,mad)),]~RNAseq_gl)
Q7
RNAseq_expr_log=log2(RNAseq_expr+1)
t.test(RNAseq_expr_log[which.max(rowSums(RNAseq_expr_log)),]~RNAseq_gl)
which.max(rowSums(RNAseq_expr_log))
t.test(RNAseq_expr_log[which.max(apply(RNAseq_expr_log,1,mad)),]~RNAseq_gl)
which.max(apply(RNAseq_expr_log,1,mad))
- 由于log不会改变大小关系,rowSums最大的基因不变,t检验结果自然也不变,不过mad值最大的基因改变了,t检验结果也相应改变了。
Q8
tmp=apply(RNAseq_expr_log,1,mad)
tmp=sort(tmp)
idex=names(tail(tmp,100))
# idex
# RNAseq_expr_log[idex,]
plot(hclust(dist(t(RNAseq_expr_log[idex,])))) #按列聚类
plot(hclust(dist(RNAseq_expr_log[idex,]))) #按行聚类
Q9
tmp=apply(RNAseq_expr_log,1,sd)
tmp=sort(tmp)
idex=names(tail(tmp,100))
# idex
# RNAseq_expr_log[idex,]
plot(hclust(dist(t(RNAseq_expr_log[idex,])))) #按列聚类
plot(hclust(dist(RNAseq_expr_log[idex,]))) #按行聚类
Q10
tmp=apply(RNAseq_expr_log,1,mad)
tmp=sort(tmp)
idex=names(tail(tmp,100))
dat=RNAseq_expr_log[idex,]
pheatmap::pheatmap(scale(dat)) #对行归一化
pheatmap::pheatmap(t(scale(t(dat)))) #对列归一化
统计检验相关
准备工作
rm(list = ls())
options(stringsAsFactors = F)
library(airway)
data("airway")
RNAseq_expr=assay(airway)
#x=RNAseq_expr[1,]
e1=RNAseq_expr[apply(RNAseq_expr,1,function(x) sum(x>0)>1),] #过滤低表达基因
colnames(RNAseq_expr)
RNAseq_gl=colData(airway)[,3]
table(RNAseq_gl)
Q1
# dim(e1)
# x=e1[5,]
# tmp=e1[1:2,]
# apply(tmp,1,function(x) t.test(x~RNAseq_gl)$p.value)
apply(e1,1,function(x) t.test(x~RNAseq_gl)$p.value)
- 问题在于t.test无法比较各项相等的两个向量
Q2
e1_a=e1[,RNAseq_gl=='trt']
e1_b=e1[,RNAseq_gl=='untrt']
a_filter=apply(e1_a, 1,function(x) sd(x)>0)
b_filter=apply(e1_b, 1,function(x) sd(x)>0)
table(a_filter,b_filter)
e1=e1[a_filter | b_filter,]
Q3
dim(e1)
apply(e1,1,function(x) t.test(x~RNAseq_gl)$p.value)
Q4
e2=log2(e1+1)
apply(e2,1,function(x) t.test(x~RNAseq_gl)$p.value)
Q5
p1=apply(e1,1,function(x) t.test(x~RNAseq_gl)$p.value)
p2=apply(e2,1,function(x) t.test(x~RNAseq_gl)$p.value)
cor(p1,p2)
最后,向大家隆重推荐生信技能树的一系列干货!
- 生信技能树超级VIP入场券:https://mp.weixin.qq.com/s/K8WiX5C7BYC3WonM2VcdHQ
- B站公益74小时生信工程师教学视频合辑:https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw
网友评论