R语言 基本数据分析

来源:互联网 发布:淘宝云客服一个月多少 编辑:程序博客网 时间:2024/03/29 23:12

原文地址:http://blog.csdn.net/abcjennifer/article/details/18997587


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

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


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

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #basic plot  
  2. boxplot(x)  
  3. qqplot(x,y)  


2.  线性拟合

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #linear regression  
  2. n = 10  
  3. x1 = rnorm(n)#variable 1  
  4. x2 = rnorm(n)#variable 2  
  5. y = rnorm(n)*3  
  6. mod = lm(y~x1+x2)  
  7. model.matrix(mod) #erect the matrix of mod  
  8. plot(mod) #plot residual and fitted of the solution, Q-Q plot and cook distance  
  9. summary(mod) #get the statistic information of the model  
  10. hatvalues(mod) #very important, for abnormal sample detection  


3. 逻辑回归

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #logistic regression  
  2. x <- c(0, 1, 2, 3, 4, 5)  
  3. y <- c(0, 9, 21, 47, 60, 63) # the number of successes  
  4. n <- 70 #the number of trails  
  5. z <- n - y #the number of failures  
  6. b <- cbind(y, z) # column bind  
  7. fitx <- glm(b~x,family = binomial) # a particular type of generalized linear model  
  8. print(fitx)  
  9.   
  10. plot(x,y,xlim=c(0,5),ylim=c(0,65)) #plot the points (x,y)  
  11.   
  12. beta0 <- fitx$coef[1]  
  13. beta1 <- fitx$coef[2]  
  14. fn <- function(x) n*exp(beta0+beta1*x)/(1+exp(beta0+beta1*x))  
  15. par(new=T)  
  16. curve(fn,0,5,ylim=c(0,60)) # plot the logistic regression curve  



3. Bootstrap采样

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. # bootstrap  
  2. # Application: 随机采样,获取最大eigenvalue占所有eigenvalue和之比,并画图显示distribution  
  3. dat = matrix(rnorm(100*5),100,5)  
  4.  no.samples = 200 #sample 200 times  
  5. # theta = matrix(rep(0,no.samples*5),no.samples,5)  
  6.  theta =rep(0,no.samples*5);  
  7.  for (i in 1:no.samples)  
  8. {  
  9.     j = sample(1:100,100,replace = TRUE)#get 100 samples each time  
  10.    datrnd = dat[j,]; #select one row each time  
  11.    lambda = princomp(datrnd)$sdev^2; #get eigenvalues  
  12. #   theta[i,] = lambda;  
  13.    theta[i] = lambda[1]/sum(lambda); #plot the ratio of the biggest eigenvalue  
  14. }  
  15.   
  16. # hist(theta[1,]) #plot the histogram of the first(biggest) eigenvalue  
  17. hist(theta); #plot the percentage distribution of the biggest eigenvalue  
  18. sd(theta)#standard deviation of theta  
  19.   
  20. #上面注释掉的语句,可以全部去掉注释并将其下一条语句注释掉,完成画最大eigenvalue分布的功能  



4. ANOVA方差分析

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #Application:判断一个自变量是否有影响 (假设我们喂3种维他命给3头猪,想看喂维他命有没有用)  
  2. #   
  3. y = rnorm(9); #weight gain by pig(Yij, i is the treatment, j is the pig_id), 一般由用户自行输入  
  4. #y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)  
  5. Treatment <- factor(c(1,2,3,1,2,3,1,2,3)) #each {1,2,3} is a group  
  6. mod = lm(y~Treatment) #linear regression  
  7. print(anova(mod))  
  8. #解释:Df(degree of freedom)  
  9. #Sum Sq: deviance (within groups, and residuals) 总偏差和  
  10. # Mean Sq: variance (within groups, and residuals) 平均方差和  
  11. # compare the contribution given by Treatment and Residual  
  12. #F value: Mean Sq(Treatment)/Mean Sq(Residuals)  
  13. #Pr(>F): p-value. 根据p-value决定是否接受Hypothesis H0:多个样本总体均数相等(检验水准为0.05)  
  14. qqnorm(mod$residual) #plot the residual approximated by mod  
  15. #如果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., 维他命对猪的体重有影响。


0 0
原创粉丝点击