R语言基本数据分析

Python019

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., 维他命对猪的体重有影响。

是指sql之类的数据库吗,可以用RODBC包与数据库连接,将数据库中的表读入R中,接下来就可以按照常规的代码解决问题了,也可以安装sqldf包,这样就可以在R中用sql语句对数据操作。install.packages("RODBC")library(RODBC)

老师的吐槽大会,乐死我了。hhh

regression,通常指用一个或者多个预测变量,也称自变量或者解释变量,来预测响应变量,也称为因变量、效标变量或者结果变量的方法

存在多个变量

AIC 考虑模型统计拟合度、用来拟合的参数数目

AIC值越小,越好

更多的变量:

图一:是否呈线性关系, 是

图二:是否呈正态分布,一条直线,正态分布

图三:位置与尺寸图,描述同方差性,如果方差不变,水平线周围的点应该是随机分布

图四:残差与杠杆图,对单个数据值的观测,鉴别离群点、高杠杆点、强影响点

模型建好,用predict函数对剩余500个样本进行预测,比较残差值,若预测准确,说明模型可以。

analysis of variance,简称ANOVA,也称为变异数分析。用于两个及两个以上样本均数差别的显著性检验。广义上,方差分析也是回归分析的一种,只不过线性回归的因变量一般是连续型变量。自变量是因子时,研究关注的重点通常会从预测转向不同组之间的差异比较。也就是方差分析。

power analysis,可以帮助在给定置信度的情况下,判断检测到给定效应值所需的样本量。也可以在给定置信度水平情况下,计算在某样本量内能检测到给定效应值的概率

拓展了线性模型的框架,包含了非正态因变量的分析。线性回归、方差分析都是基于正态分布的假设

-泊松回归 ,用来为计数资料和列联表建模的一种回归分析。泊松回归假设因变量是泊松分布,并假设它平均值的对数可被未知参数的线性组合建模

-logistic 回归

通过一系列连续型或者类别型预测变量来预测二值型结果变量是,logistic 回归是一个非常有用的工具。流行病学研究中用的多。

Principal Component Analysis,PCA,探索和简化多变量复杂关系的常用方法。 是一种数据降维技巧。可以将大量相关变量转化为一组很少的不相关变量。这些无关变量成为主成分。主成分是对原始变量重新进行线性组合,将原先众多具有一定相关性的指标,重新组合为一组的心得相互独立的综合指标。

探索性因子分析法 exploratory factor analysis,简称为EFA,是一系列用来发现一组变量的潜在结构的方法。通过找寻一组更小的、潜在的活隐藏的结构来解释已观测到的、显式的变量间的关系

因子分析步骤与PCA一致

啤酒与尿布