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 timedatrnd = dat[j,]; #select one row each timelambda = 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., 维他命对猪的体重有影响。
关注数据统计分析,欢迎一起讨论。微博 Rachel____Zhang
R语言 基本数据分析相关推荐
- R语言与数据分析练习:使用ARIMA模型预测网站访问量
R语言与数据分析练习:使用ARIMA模型预测网站访问量 使用ARIMA模型预测网站访问量 一.实验背景: 随着流量的增大,某网站的数据信息量也在以一定的幅度增长 基于该网站2016年9月~2017年2 ...
- R语言对数线性模型loglm函数_用R语言进行数据分析:常规和广义线性模型
用R语言进行数据分析:常规和广义线性模型 线性模型 对于常规的多重模型(multiple model)拟合,最基本的函数是lm(). 下面是调用它的方式的一种改进版: >fitted.model ...
- R语言 面板数据分析 plm包实现(一) ——LSDV和固定效应模型
系列文章 R做面板数据分析:R语言 面板数据分析 plm包实现(一) --LSDV和固定效应模型 如果想看随机效应模型怎么做,参见这篇文章 R语言 面板数据分析 plm包实现(二)--随机效应模型 如 ...
- 2014全球软件技术峰会WOT:R语言金融数据分析
跨界知识聚会系列文章,"知识是用来分享和传承的",各种会议.论坛.沙龙都是分享知识的绝佳场所.我也有幸作为演讲嘉宾参加了一些国内的大型会议,向大家展示我所做的一些成果.从听众到演讲 ...
- 【R语言与数据分析实战】R软件编程
目录 1.流程控制 (1) if语句 (2) 循环语句 2.运算 (1) 数值运算 (2) 向量运算 (3) NA处理 3.定义函数 (1) 可变长函数 (2) 嵌套函数 4.作用域 5.对象的不变性 ...
- 【R语言与数据分析实战】绘图
目录 1.散点图 2.图像选项 2.1 坐标轴名称 2.2 图形标题 2.3 点的类型 2.4 点的大小 2.5 颜色 2.6 坐标轴的取值范围 2.7 图像类型 2.8 线型 2.9 图形排列 2. ...
- 看书标记【R语言 商务数据分析实战4】
看书标记--关于R语言 chapter 4 4.2 任务实现 [R语言 商务数据分析实战4] chapter 4 财政收入预测分析 从海量数据中发现隐藏的运行模式,并提供具有决策意义的信息.变量过多会 ...
- 看书标记【R语言 商务数据分析实战5】
看书标记--关于R语言 chapter 5 5.2 任务实现 [R语言 商务数据分析实战5] chapter 5 金融服务机构资金流量预测 数据理解和预处理>>检验平稳性+纯随机性> ...
- 看书标记【R语言 商务数据分析实战6】
看书标记--关于R语言 chapter 6 6.2 任务实 [R语言 商务数据分析实战6] chapter 6 P2P信用贷款风险控制(用户逾期还款概率模型) 关于数据库的应用+数据清洗+实时数据识别 ...
- 看书标记【R语言 商务数据分析实战9】
看书标记--关于R语言 chapter 9 9.2 任务实现 [R语言 商务数据分析实战9] chapter 9 餐饮企业综合分析 统计分析>>ARIMA预测销售额>>协同过滤 ...
最新文章
- Ubuntu Server 16.04服务器版配置图解教程06 - 安装MySql
- VS修改项目名后出现的提示(warning MSB8028: The intermediate directory (Debug\) contains files shared from anoth)
- 《jQuery、jQuery UI及jQuery Mobile技巧与示例》——3.3 技巧:生成类名
- python中ipo模型有_python ipo模型是指什么?_后端开发
- 接口隔离原则_设计模式的三大分类及六大原则
- explain for connection用法
- Apache+Php+Mariadb+NFS+discuz
- linux开发板最好芯片,既然是中国十大最有影响力的芯片之一 一定要有个好开发板...
- mysql关联子查询_MySQL 关联子查询
- java读取properties文件连接数据库
- python爬取下厨房每周最受欢迎菜谱
- Python爬虫:7_BeautifulSoup4图片爬取
- AJP:纵向研究:抑郁症青年在当前期和累积期的奖赏系统的功能障碍具有不同的神经基础
- Tengine全面支持寒武纪思元芯片平台,共同推进AI芯片加速之路
- 哥德巴赫猜想两种题目解法
- 大数据毕设 地铁客流分析与可视化系统
- 中职生职业生涯规划书2000字学计算机,中职生职业生涯规划书计算机专业
- 统计Textarea中字数的方法
- 曼尼托巴大学计算机科学硕士,曼尼托巴大学计算机科学本科申请.pdf
- 兼职币圈代言人?特斯拉CEO:加密货币是我的安全词
热门文章
- 命名实体识别学习-用lstm+crf处理conll03数据集
- 作为一名程序员,如何开展自己的副业?月赚三万的真实故事
- talend导出数据到oracle,Talend使用步骤.pdf
- 蛙蛙推荐:蛙蛙牌asp上传组件,(有完整注释)
- Python数据分析必知必会——TGI指数
- 【Midjourney】Midjourney 基本操作 ① ( 使用 Midjourney 生成图片 | V 按钮继续生成 | U 按钮获取结果 | Midjourney 设置面板 )
- 自己的CRT (一)
- ansible 快速部署 kubeadm 离线安装集群
- 学python心得体会1000字-浅谈我对python的学习感受
- Navicat12安装(图文教程)