
本文基于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
SQL Server 中 CONVERT 函数的日期转换:从基础用法到实战优化 在 SQL Server 的数据处理中,日期格式转换是高频需求 —— 无论 ...
2025-09-18MySQL 大表拆分与关联查询效率:打破 “拆分必慢” 的认知误区 在 MySQL 数据库管理中,“大表” 始终是性能优化绕不开的话题。 ...
2025-09-18CDA 数据分析师:表结构数据 “获取 - 加工 - 使用” 全流程的赋能者 表结构数据(如数据库表、Excel 表、CSV 文件)是企业数字 ...
2025-09-18DSGE 模型中的 Et:理性预期算子的内涵、作用与应用解析 动态随机一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明确:TIF 中的地名有哪两种存在形式? 在开始提取前,需先判断 TIF 文件的类型 —— ...
2025-09-17CDA 数据分析师:解锁表结构数据特征价值的专业核心 表结构数据(以 “行 - 列” 规范存储的结构化数据,如数据库表、Excel 表、 ...
2025-09-17Excel 导入数据含缺失值?详解 dropna 函数的功能与实战应用 在用 Python(如 pandas 库)处理 Excel 数据时,“缺失值” 是高频 ...
2025-09-16深入解析卡方检验与 t 检验:差异、适用场景与实践应用 在数据分析与统计学领域,假设检验是验证研究假设、判断数据差异是否 “ ...
2025-09-16CDA 数据分析师:掌控表格结构数据全功能周期的专业操盘手 表格结构数据(以 “行 - 列” 存储的结构化数据,如 Excel 表、数据 ...
2025-09-16MySQL 执行计划中 rows 数量的准确性解析:原理、影响因素与优化 在 MySQL SQL 调优中,EXPLAIN执行计划是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 对象的 text 与 content:区别、场景与实践指南 在 Python 进行 HTTP 网络请求开发时(如使用requests ...
2025-09-15CDA 数据分析师:激活表格结构数据价值的核心操盘手 表格结构数据(如 Excel 表格、数据库表)是企业最基础、最核心的数据形态 ...
2025-09-15Python HTTP 请求工具对比:urllib.request 与 requests 的核心差异与选择指南 在 Python 处理 HTTP 请求(如接口调用、数据爬取 ...
2025-09-12解决 pd.read_csv 读取长浮点数据的科学计数法问题 为帮助 Python 数据从业者解决pd.read_csv读取长浮点数据时的科学计数法问题 ...
2025-09-12CDA 数据分析师:业务数据分析步骤的落地者与价值优化者 业务数据分析是企业解决日常运营问题、提升执行效率的核心手段,其价值 ...
2025-09-12用 SQL 验证业务逻辑:从规则拆解到数据把关的实战指南 在业务系统落地过程中,“业务逻辑” 是连接 “需求设计” 与 “用户体验 ...
2025-09-11塔吉特百货孕妇营销案例:数据驱动下的精准零售革命与启示 在零售行业 “流量红利见顶” 的当下,精准营销成为企业突围的核心方 ...
2025-09-11CDA 数据分析师与战略 / 业务数据分析:概念辨析与协同价值 在数据驱动决策的体系中,“战略数据分析”“业务数据分析” 是企业 ...
2025-09-11Excel 数据聚类分析:从操作实践到业务价值挖掘 在数据分析场景中,聚类分析作为 “无监督分组” 的核心工具,能从杂乱数据中挖 ...
2025-09-10统计模型的核心目的:从数据解读到决策支撑的价值导向 统计模型作为数据分析的核心工具,并非简单的 “公式堆砌”,而是围绕特定 ...
2025-09-10