
本文基于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
# 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., 维他命对猪的体重有影响。
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
2025 年,数据如同数字时代的 DNA,编码着人类社会的未来图景,驱动着商业时代的运转。从全球互联网用户每天产生的2.5亿TB数据, ...
2025-06-052025 年,数据如同数字时代的 DNA,编码着人类社会的未来图景,驱动着商业时代的运转。从全球互联网用户每天产生的2.5亿TB数据, ...
2025-05-27CDA数据分析师证书考试体系(更新于2025年05月22日)
2025-05-26解码数据基因:从数字敏感度到逻辑思维 每当看到超市货架上商品的排列变化,你是否会联想到背后的销售数据波动?三年前在零售行 ...
2025-05-23在本文中,我们将探讨 AI 为何能够加速数据分析、如何在每个步骤中实现数据分析自动化以及使用哪些工具。 数据分析中的AI是什么 ...
2025-05-20当数据遇见人生:我的第一个分析项目 记得三年前接手第一个数据分析项目时,我面对Excel里密密麻麻的销售数据手足无措。那些跳动 ...
2025-05-20在数字化运营的时代,企业每天都在产生海量数据:用户点击行为、商品销售记录、广告投放反馈…… 这些数据就像散落的拼图,而相 ...
2025-05-19在当今数字化营销时代,小红书作为国内领先的社交电商平台,其销售数据蕴含着巨大的商业价值。通过对小红书销售数据的深入分析, ...
2025-05-16Excel作为最常用的数据分析工具,有没有什么工具可以帮助我们快速地使用excel表格,只要轻松几步甚至输入几项指令就能搞定呢? ...
2025-05-15数据,如同无形的燃料,驱动着现代社会的运转。从全球互联网用户每天产生的2.5亿TB数据,到制造业的传感器、金融交易 ...
2025-05-15大数据是什么_数据分析师培训 其实,现在的大数据指的并不仅仅是海量数据,更准确而言是对大数据分析的方法。传统的数 ...
2025-05-14CDA持证人简介: 万木,CDA L1持证人,某电商中厂BI工程师 ,5年数据经验1年BI内训师,高级数据分析师,拥有丰富的行业经验。 ...
2025-05-13CDA持证人简介: 王明月 ,CDA 数据分析师二级持证人,2年数据产品工作经验,管理学博士在读。 学习入口:https://edu.cda.cn/g ...
2025-05-12CDA持证人简介: 杨贞玺 ,CDA一级持证人,郑州大学情报学硕士研究生,某上市公司数据分析师。 学习入口:https://edu.cda.cn/g ...
2025-05-09CDA持证人简介 程靖 CDA会员大咖,畅销书《小白学产品》作者,13年顶级互联网公司产品经理相关经验,曾在百度、美团、阿里等 ...
2025-05-07相信很多做数据分析的小伙伴,都接到过一些高阶的数据分析需求,实现的过程需要用到一些数据获取,数据清洗转换,建模方法等,这 ...
2025-05-06以下的文章内容来源于刘静老师的专栏,如果您想阅读专栏《10大业务分析模型突破业务瓶颈》,点击下方链接 https://edu.cda.cn/g ...
2025-04-30CDA持证人简介: 邱立峰 CDA 数据分析师二级持证人,数字化转型专家,数据治理专家,高级数据分析师,拥有丰富的行业经验。 ...
2025-04-29CDA持证人简介: 程靖 CDA会员大咖,畅销书《小白学产品》作者,13年顶级互联网公司产品经理相关经验,曾在百度,美团,阿里等 ...
2025-04-28CDA持证人简介: 居瑜 ,CDA一级持证人国企财务经理,13年财务管理运营经验,在数据分析就业和实践经验方面有着丰富的积累和经 ...
2025-04-27