横截面数据分类——基于R

参考资料:

《复杂数据统计方法》&网络&帮助文件

  适用情况:在因变量为分类变量而自变量含有多个分类变量或分类变量水平较多的情况。

一.

(一)概论和例子

  数据来源:http://archive.ics.uci.edu/ml/datasets/Cardiotocography

自变量:LB - FHR baseline (beats per minute)

AC - # of accelerations per second
FM - # of fetal movements per second
UC - # of uterine contractions per second
DL - # of light decelerations per second
DS - # of severe decelerations per second
DP - # of prolongued decelerations per second
ASTV - percentage of time with abnormal short term variability
MSTV - mean value of short term variability
ALTV - percentage of time with abnormal long term variability
MLTV - mean value of long term variability
Width - width of FHR histogram
Min - minimum of FHR histogram
Max - Maximum of FHR histogram
Nmax - # of histogram peaks
Nzeros - # of histogram zeros
Mode - histogram mode
Mean - histogram mean
Median - histogram median
Variance - histogram variance
Tendency - histogram tendency
CLASS - FHR pattern class code (1 to 10)

因变量:

NSP - fetal state class code (N=normal; S=suspect; P=pathologic)

(二)产生交叉验证数据集

1.十折交叉验证 概念(百度百科)

  英文名叫做10-fold cross-validation,用来测试算法准确性。是常用的测试方法。将数据集分成十分,轮流将其中9份作为训练数据,1份作为测试数据,进行试验。每次试验都会得出相应的正确率(或差错率)。10次的结果的正确率(或差错率)的平均值作为对算法精度的估计,一般还需要进行多次10折交叉验证(例如10次10折交叉验证),再求其均值,作为对算法准确性的估计。
之所以选择将数据集分为10份,是因为通过利用大量数据集、使用不同学习技术进行的大量试验,表明10折是获得最好误差估计的恰当选择,而且也有一些理论根据可以证明这一点。但这并非最终诊断,争议仍然存在。而且似乎5折或者20折与10折所得出的结果也相差无几。

Fold=function(Z=10,w,D,seed=7777){
n=nrow(w)
d=1:n
dd=list()
e=levels(w[,D])
T=length(e)
set.seed(seed)
for(i in 1:T){
d0=d[w[,D]==e[i]]
j=length(d0)
ZT=rep(1:Z,ceiling(j/Z))[1:j]
id=cbind(sample(ZT,length(ZT)),d0)
dd[[i]]=id}
mm=list()
for(i in 1:Z){u=NULL;
for(j in 1:T)u=c(u,dd[[j]][dd[[j]][,1]==i,2])
mm[[i]]=u}
return(mm)}
#读入数据
w=read.csv("CTG.NAOMIT.csv")
#因子化最后三个哑元变量
F=21:23 #三个分类变量的列数
for(i in F)
w[,i]=factor(w[,i])
D=23 #因变量的位置
Z=10 #折数
n=nrow(w)#行数
mm=Fold(Z,w,D,8888)

二.决策树分类(分类树)

library(rpart.plot)
(a=rpart(NSP~.,w))#用决策树你和全部数据并打印输出
rpart.plot(a,type=2,extra=4)

rpart.plot参数解释:

x  :

An rpart object. The only required argument.

type:

Type of plot. Five possibilities:

0 The default. Draw a split label at each split and a node label at each leaf.

1 Label all nodes, not just leaves. Similar to text.rpart‘s all=TRUE.

2 Like 1 but draw the split labels below the node labels. Similar to the plots in the CART book.

3 Draw separate split labels for the left and right directions.

4 Like 3 but label all nodes, not just leaves. Similar to text.rpart‘s fancy=TRUE. See also clip.right.labs.

extra :

Display extra information at the nodes. Possible values:

0 No extra information (the default).

1 Display the number of observations that fall in the node (per class for class objects; prefixed by the number of events for poisson and exp models). Similar to text.rpart‘s use.n=TRUE.

2 Class models: display the classification rate at the node, expressed as the number of correct classifications and the number of observations in the node. Poisson and exp models: display the number of events.

3 Class models: misclassification rate at the node, expressed as the number of incorrect classifications and the number of observations in the node.

4 Class models: probability per class of observations in the node (conditioned on the node, sum across a node is 1).

5 Class models: like 4 but do not display the fitted class.

6 Class models: the probability of the second class only. Useful for binary responses.

7 Class models: like 6 but do not display the fitted class.

8 Class models: the probability of the fitted class.

9 Class models: the probabilities times the fraction of observations in the node (the probability relative to all observations, sum across all leaves is 1).

branch:  

Controls the shape of the branch lines. Specify a value between 0 (V shaped branches) and 1 (square shouldered branches). Default is if(fallen.leaves) 1 else .2.

branch=0

branch=1

digits  :

The number of significant digits in displayed numbers. Default 2.

rpart.plot(a,extra=4,digits=4)

时间: 2024-08-04 11:44:22

横截面数据分类——基于R的相关文章

中文分词实践(基于R语言)

背景:分析用户在世界杯期间讨论最多的话题. 思路:把用户关于世界杯的帖子拉下来,然后做中文分词+词频统计,最后将统计结果简单做个标签云,效果如下: 后续:中文分词是中文信息处理的基础,分词之后,其实还有特别多有趣的文本挖掘工作可以做,也是个知识发现的过程,以后有机会再学习下. ================================================== * 中文分词常用实现: 单机:R语言+Rwordseg分词包 (建议数据量<1G) 分布式:Hadoop+Smallse

递归函数之阶乘和字符串反转-基于R和Python

Python课第五周开始讲函数了.递归函数.递归在python中不能超过900多层,否则报错内存溢出什么的.同样在R中递归太深也会报错,阈值和python中大概一样,900多次就报错了. error message: 错误: 评估嵌套太深:无穷递归/ options(expressions=)?收捲时出错: 评估嵌套太深:无穷递归/ options(expressions=)? 基于Python # 递归函数 阶乘 def fact(n): if n==0: return 1 else: ret

分享《深度学习精要(基于R语言)》+PDF+源码+Joshua F.Wiley+高蓉

下载:https://pan.baidu.com/s/14UlxD5VJRY92UpP7Wr6Taw 更多最新的资料:http://blog.51cto.com/14087171 <深度学习精要(基于R语言)>高清中文版PDF+高清英文版PDF+源代码 高清中文版PDF,带目录和书签,能够复制粘贴:高清英文版PDF,带目录和书签,能够复制粘贴:中英文两版可以对比学习. 配套源代码: 经典书籍,讲解详细: 其中高清中文版如图 原文地址:http://blog.51cto.com/14087171

逻辑回归算法实现_基于R语言

逻辑回归(Logistic Regression)模型和线性回归非常相似,可以说就是在逻辑回归的基础上加上了一步逻辑转换,也就是因为这个转换,使逻辑回归模型非常适用于二分类问题的概率预测.本文主要详述逻辑回归模型的基础以及逻辑回归模型的R语言实现. 一.逻辑回归模型原理 首先要讲一下线性回归在预测分类事件中的缺点:线性回归模型的泛化能力很差,如果训练集存在噪点,会导致模型的结果特别差,不同样本建立起来的模型分割点不同:下图中根据年龄预测是否成年的分类问题,斜线就是根据训练集拟合出来的线性回归模型

分类算法简介 基于R

最近的关键字:分类算法,outlier detection, machine learning 简介: 此文将 k-means,decision tree,random forest,SVM(support vector mechine),人工神经网络(Artificial Neural Network,简称ANN )这几种常见的算法 apply 在同一个数据集 spam,看各种方法预测错误率,或准确率,旨在追求预测准确性,辨识出这几种方法的实用性,对背后的理论依据,大量的数学公式,不作讨论(能

基于R语言的用户分析

1. 基本分析理论 C5.0是决策树模型中的算法,79年由J R Quinlan发展,并提出了ID3算法,主要针对离散型属性数据,其后又不断的改进,形成C4.5,它在ID3基础上增加了队连续属性的离散化.C5.0是C4.5应用于大数据集上的分类算法,主要在执行效率和内存使用方面进行了改进.C4.5算法是ID3算法的修订版,采用GainRatio来加以改进方法,选取有最大GainRatio的分割变量作为准则,避免ID3算法过度配适的问题.C5.0算法则是C4.5算法的修订版,适用于处理大数据集,采

机器学习-线性回归(基于R语言)

基本概念 利用线性的方法,模拟因变量与一个或多个自变量之间的关系.自变量是模型输入值,因变量是模型基于自变量的输出值. 因变量是自变量线性叠加和的结果. 线性回归模型背后的逻辑——最小二乘法计算线性系数 最小二乘法怎么理解? 它的主要思想就是求解未知参数,使得理论值与观测值之差(即误差,或者说残差)的平方和达到最小.在这里模型就是理论值,点为观测值.使得拟合对象无限接近目标对象. 一元线性回归与多元线性回归 自变量只有一个的时候叫一元线性回归,自变量有多个时候叫多元线性回归. R语言实现 bik

开心消消乐 - 基于R

1 #let's begin, generate a sample with 5 kinds of animals 2 begin=sample(1:5,100,replace = TRUE) 3 4 #generate n random numbers come from 5 categories 5 r=function(n){ 6 r=sample(1:5,n,replace = TRUE) 7 } 8 9 #define play styles 10 play.row=function(

基于R语言的数据分析和挖掘方法总结——均值检验

2.1 单组样本均值t检验(One-sample t-test) 2.1.1 方法简介 t检验,又称学生t(student t)检验,是由英国统计学家戈斯特(William Sealy Gosset, 1876-1937)所提出,student则是他的笔名.t检验是一种检验总体均值的统计方法,当数据中仅含单组样本且样本数较大时(通常样本个数≧30的样本可视为样本数较大),可用这种方法来检验总体均值是否大于.小于或等于某一特定数值.当数据中仅含单组样本但样本数较小时(通常样本个数<30的样本可视为