R语言基本数据分析

Python013

R语言基本数据分析,第1张

R语言基本数据分析

本文基于R语言进行基本数据统计分析,包括基本作图,线性拟合,逻辑回归,bootstrap采样和Anova方差分析的实现及应用。

不多说,直接上代码,代码中有注释。

1. 基本作图(盒图,qq图)

#basic plot

boxplot(x)

qqplot(x,y)

2. 线性拟合

#linear regression

n = 10

x1 = rnorm(n)#variable 1

x2 = rnorm(n)#variable 2

y = rnorm(n)*3

mod = lm(y~x1+x2)

model.matrix(mod) #erect the matrix of mod

plot(mod) #plot residual and fitted of the solution, Q-Q plot and cook distance

summary(mod) #get the statistic information of the model

hatvalues(mod) #very important, for abnormal sample detection

3. 逻辑回归

#logistic regression

x <- c(0, 1, 2, 3, 4, 5)

y <- c(0, 9, 21, 47, 60, 63) # the number of successes

n <- 70 #the number of trails

z <- n - y #the number of failures

b <- cbind(y, z) # column bind

fitx <- glm(b~x,family = binomial) # a particular type of generalized linear model

print(fitx)

plot(x,y,xlim=c(0,5),ylim=c(0,65)) #plot the points (x,y)

beta0 <- fitx$coef[1]

beta1 <- fitx$coef[2]

fn <- function(x) n*exp(beta0+beta1*x)/(1+exp(beta0+beta1*x))

par(new=T)

curve(fn,0,5,ylim=c(0,60)) # plot the logistic regression curve

3. Bootstrap采样

# bootstrap

# Application: 随机采样,获取最大eigenvalue占所有eigenvalue和之比,并画图显示distribution

dat = matrix(rnorm(100*5),100,5)

no.samples = 200 #sample 200 times

# theta = matrix(rep(0,no.samples*5),no.samples,5)

theta =rep(0,no.samples*5)

for (i in 1:no.samples)

{

j = sample(1:100,100,replace = TRUE)#get 100 samples each time

datrnd = dat[j,]#select one row each time

lambda = princomp(datrnd)$sdev^2#get eigenvalues

# theta[i,] = lambda

theta[i] = lambda[1]/sum(lambda)#plot the ratio of the biggest eigenvalue

}

# hist(theta[1,]) #plot the histogram of the first(biggest) eigenvalue

hist(theta)#plot the percentage distribution of the biggest eigenvalue

sd(theta)#standard deviation of theta

#上面注释掉的语句,可以全部去掉注释并将其下一条语句注释掉,完成画最大eigenvalue分布的功能

4. ANOVA方差分析

#Application:判断一个自变量是否有影响 (假设我们喂3种维他命给3头猪,想看喂维他命有没有用)

#

y = rnorm(9)#weight gain by pig(Yij, i is the treatment, j is the pig_id), 一般由用户自行输入

#y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)

Treatment <- factor(c(1,2,3,1,2,3,1,2,3)) #each {1,2,3} is a group

mod = lm(y~Treatment) #linear regression

print(anova(mod))

#解释:Df(degree of freedom)

#Sum Sq: deviance (within groups, and residuals) 总偏差和

# Mean Sq: variance (within groups, and residuals) 平均方差和

# compare the contribution given by Treatment and Residual

#F value: Mean Sq(Treatment)/Mean Sq(Residuals)

#Pr(>F): p-value. 根据p-value决定是否接受Hypothesis H0:多个样本总体均数相等(检验水准为0.05)

qqnorm(mod$residual) #plot the residual approximated by mod

#如果qqnorm of residual像一条直线,说明residual符合正态分布,也就是说Treatment带来的contribution很小,也就是说Treatment无法带来收益(多喂维他命少喂维他命没区别)

如下面两图分别是

(左)用 y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)和

(右)y = rnorm(9)

的结果。可见如果给定猪吃维他命2后体重特别突出的数据结果后,qq图种residual不在是一条直线,换句话说residual不再符合正态分布,i.e., 维他命对猪的体重有影响。

杀杀

记录一些R语言读入数据的方法还有可能遇到的问题~

读入数据时,需要先了解数据文件的类型(也就是看后缀)。一般就能够知道数据的类型和分隔符等信息。

另外,如果能够用excel预览一下数据的话,可以先看看数据是否有行列名。有些数据会有两列的行名,如基因名-基因id-表达值······,特殊的数据需要额外的处理。

还需要注意一下matrix和data.frame的数据结构,matrix中只能有一种数据类型,这意味着如果在读入数据时不进行合适的处理,R会将数值强行读成字符型,造成读数据的错误。

当用excel存储过之后,再用R处理时,会提示你行名重复,其实根本没有重复。因此建议不要用excel保存这种数据,一定要编辑可以使用notepad++或者ultra edit等软件。

-----正题分割线-----

read.xx的函数是R的内置函数,可以直接读取,并且设置一些参数

这些函数读取后都默认为data.frame,如果需要矩阵请使用as.matrix转换。

一定要赋值,不然R语言会把大大的矩阵print出来。

如果是没怎么见过的类型:

这个函数会自动识别你的分隔符,并且把第一行设为列名,但是没办法指定行名,需要读入以后自己设置

跟read.delim类似,可以读各种类型的文件以及非常大的文件:

读取后默认是一种data.table的数据类型,需要通过as.matrix/as.data.frame转换后使用。

像perl语言一样,逐行读取数据具有很大的优势

(万一文件超多行对吧)对于那种几个G的文件,全部读进来可能会导致你的电脑死机,所以我们可以先读几百行进来看看,或者分批读取,这样不会占用电脑太大内存,读取方法和上文的一次性读入有所不同-随便找个文件举例:

接下来继续读入数据,比如说我现在想读4行,因为文件是txt类型,所以分隔设为\t

第一种:把excel中所有sheet的表格读入为data.frame,并分别命名为每个sheet的名称

---请忽略硬核打码

第二种:把excel中所有sheet的表格读入为矩阵,并放进一个list中

R语言批量读文件

批量读excel的xlsx文件原理是和读其它文件一样的。

学到了新的会持续更新哟~

r语言数据分析是查看数据的结构、类型,数据处理。根据查询相关资料信息显示:R语言是一个开源、跨平台的科学计算和统计分析软件包,具有丰富多样、强大的的统计功能和数据分析功能,数据可视化可以绘制直方图、箱型图、小提琴图等展示分数的分布情况可以通过散点图和线性拟合来展示分数和年龄之间的关系。