R常用代码 [Inkit wiki]

用户工具

站点工具


stat:3descntests:rsnipp

R常用代码

# 变量赋值
x1<-c(3,4,5,6)     #变量赋值等于把数据储存到程序中,与SPSS打开数据文件意义是类似的。所以运行统计运算代码之前必须对变量进行赋值。
x2<-c(7,8,9,5)     #我们经常有2~3组数,所以分别存在不同的变量中以区分其分组。
x3<-c(11,15,17,19)     #根据题目修改括号()中的数值即可。

# 统计描述
x1<-c(3,4,5,6)
summary(x1)  # 综合性统计描述
psych::describe(x1)  # 综合性统计描述
mean(x1)   #均值
median(x1)  #中位数
sd(x1)   #标准差
var(x1)   #方差
quantile(x1,c(0.25,0.75))   #P25与P75
IQR(x1)   #四分位间距


# 正态性检验
x1<-c(3,4,5,6)
shapiro.test(x1)

# 方差齐性检验 (F检验)
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
var.test(x1,x2)

# 方差齐性检验 (Levene检验)
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
x3<-c(11,15,17,19)
df<-rbind(data.frame(x=x1,group=as.factor(1)), data.frame(x=x2,group=as.factor(2)), data.frame(x=x3,group=as.factor(3)))  # 将数据整理成独立样本设计的表格
car::leveneTest(df$x,df$group)

# 单样本t检验,总体均数μ=3
x1<-c(3,4,5,6)
t.test(x1,mu=3)

# 配对设计求差值,并检验正态性
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
d<- x1-x2
shapiro.test(d)

# 配对样本t检验
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
t.test(x1, x2, paired = TRUE)

# 两独立样本t检验 方差齐
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
t.test(x1, x2, paired = FALSE, var.equal = TRUE)

# 两独立样本t检验 方差齐
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
t.test(x1, x2, paired = FALSE, var.equal = FALSE)


#方差分析
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
x3<-c(11,15,17,19)
df<-rbind(data.frame(x=x1,group=as.factor(1)),data.frame(x=x2,group=as.factor(2)),data.frame(x=x3,group=as.factor(3)))  # 将数据整理成独立样本设计的表格
model<-aov(x~group,data = df)  # 单因素方差分析
summary(model)  #显示方差分析表


# Dunnett 法
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
x3<-c(11,15,17,19)
df<-rbind(data.frame(x=x1,group=as.factor(1)),data.frame(x=x2,group=as.factor(2)),data.frame(x=x3,group=as.factor(3)))  # 将数据整理成独立样本设计的表格
model <- aov(x ~ group, df) # 单因素方差分析
rht <- multcomp::glht(model, linfct = multcomp::mcp(group = 'Dunnett'), alternative = 'two.side') # 对方差分析结果进行两两比较
summary(rht)
plot(rht)   #还可以画个图,可选


# SNK 法
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
x3<-c(11,15,17,19)
df<-rbind(data.frame(x=x1,group=as.factor(1)),data.frame(x=x2,group=as.factor(2)),data.frame(x=x3,group=as.factor(3)))  # 将数据整理成独立样本设计的表格
model <- aov(x ~ group, df)  # 单因素方差分析
out <- agricolae::SNK.test(model,"group")    # 对方差分析结果进行SNK两两比较
out$group   #显示结果
plot(out)    #还可以画个图,可选

# 配对四格表的卡方检验
mcnemar.test(matrix(c(46,18,6,8), nrow=2))   

# 独立样本四格表的卡方检验
x=matrix(c(46,18,6,8),nrow=2,ncol=2)   #建立四格表
chisq.test(x)$expected   #显示理论频数
chisq.test(x)

# Fisher确切概率法
x=matrix(c(46,18,6,8),nrow=2,ncol=2)
fisher.test(x)


# 两独立样本秩和检验
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
df<-rbind(data.frame(x=x1,group=as.factor(1)),data.frame(x=x2,group=as.factor(2)))  # 将数据整理成独立样本设计的表格
wilcox.test(x ~ group, df, alternative = "two.sided",exact = FALSE,paired =FALSE)


# 配对秩和检验
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
df<-rbind(data.frame(x=x1,group=as.factor(1)),data.frame(x=x2,group=as.factor(2)))  # 将数据整理成独立样本设计的表格
wilcox.test(x ~ group, df, alternative = "two.sided",exact = FALSE,paired =TRUE)


# 多组的秩和检验
x1<-c(3,4,5,6)
x2<-c(7,8,9,5)
x3<-c(11,15,17,19)
df<-rbind(data.frame(x=x1,group=as.factor(1)),data.frame(x=x2,group=as.factor(2)),data.frame(x=x3,group=as.factor(3)))  # 将数据整理成独立样本设计的表格
kruskal.test(x ~ group, df)
stat/3descntests/rsnipp.txt · 最后更改: 2023/04/04 18:02 由 127.0.0.1