《统计建模与R软件》章节试读

当前位置:首页 > 社会科学 > > 统计建模与R软件章节试读

出版社:清华大学出版社
出版日期:2007-5
ISBN:9787302143666
作者:薛毅
页数:525页

《统计建模与R软件》的笔记-第63页 - 2.2.5

> paste(c('a','b'),collapse='.')
[1] "a.b"
paste {base} R Documentation
Concatenate Strings
Description
Concatenate vectors after converting to character.
Usage
paste (..., sep = " ", collapse = NULL)
paste0(..., collapse = NULL)
Arguments
...
one or more R objects, to be converted to character vectors.
sep
a character string to separate the terms. Not NA_character_.
collapse
an optional character string to separate the results. Not NA_character_.
Details
paste converts its arguments (via as.character) to character strings, and concatenates them (separating them by the string given by sep). If the arguments are vectors, they are concatenated term-by-term to give a character vector result. Vector arguments are recycled as needed, with zero-length arguments being recycled to "".
Note that paste() coerces NA_character_, the character missing value, to "NA" which may seem undesirable, e.g., when pasting two character vectors, or very desirable, e.g. in paste("the value of p is ", p).
paste0(..., collapse) is equivalent to paste(..., sep = "", collapse), slightly more efficiently.
If a value is specified for collapse, the values in the result are then concatenated into a single string, with the elements being separated by the value of collapse.
> paste(1:10) #same as as.character(1:10)
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"> paste(1:10,collapse='s')
[1] "1s2s3s4s5s6s7s8s9s10"
> paste(1:10,collapse="s")
[1] "1s2s3s4s5s6s7s8s9s10"
注意不同。书中的用法,1-10分别作为一个字符串,一共10个。而加上collapse,则连成了一个长字符串。
以下的代码和图片或许解释更清楚(通过RStudio):
> x<-paste(1:10);x
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
> y<-paste(1:10,collapse='s');y
[1] "1s2s3s4s5s6s7s8s9s10"书中的用法,1-10分别作为一个字符串,一共10个。而加上collapse,则连成了一个长字符串。
根据R文档,paste(1:10)被转换为字符;而paste(1:10,collapse='s')则把结果里所有字符串连接起来成一个大字符串,用s来间隔。而以下结果为之所以没有被s连接,因为本身 "1 2 3 4 5"就是唯一的结果字符串:
> x1<-paste(1,2,3,4,5,collapse='s');x1
[1] "1 2 3 4 5"
> x2<-paste("1","2","3","4","5",collapse='s');x2
[1] "1 2 3 4 5"

《统计建模与R软件》的笔记-第203页 - 假设检验

test of hypothesis
概念:
零假设、原假设(null hypothesis)
备择假设(alternative hypothesis)
单边检验VS双边检验
一尾VS二尾
假设检验的依据是样本。样本的某些取值可能对原假设H0有利,而另一些取值可能对H0不利,因此可以根据某种公认的合理准则讲样本空间分成两部分。一部分是拒绝域(critical region),当样本落入拒绝域时,便拒绝H0另一部分可称为接受域(acceptance region)
重要的参数检验
Z,t
正态分布为例,给定z值之后,需要考虑X≥|z|和X≤|z|的概率P值
P_value<-function(cdf, x, paramet=numeric(0), side=0){
n<-length(paramet)
P<-switch(n+1,
cdf(x),
cdf(x, paramet),
cdf(x, paramet[1], paramet[2]),
cdf(x, paramet[1], paramet[2], paramet[3])
)
if (side<0) P
else if (side>0) 1-P
else
if (P<1/2) 2*P
else 2*(1-P)
}
cdf是分布函数,正态分布是pnorm,x是计算P值的给定值。paramet是对应的分布参数,如正太分布参数是paramet=c(mu,sigma).side是计算单侧P值或者双侧P值的指标参数,side=-1,计算左侧的P值,输入side=1,计算右侧的P值,输入side=0或默认,计算两侧P值。函数输出值是相应的P值。
在得到P值后,其检验标准改为:当P值小于指定的显著水平a时,则拒绝原假设,否则不拒绝。

《统计建模与R软件》的笔记-第80页 - R软件的使用-1

2.1R软件使用
#是说明语句字符,后面的语句是说明语句,增加程序可读性;
<-表示赋值,c()表示数组,x<-c(....)表示将一组数据赋给变量X,
平均数:mean(x) ,标准差sd(x);散点图plot(x);直方图hist(x);
阅读文件:
rt<-read.table(" exam0203.txt", head=TRUE);rt
head=TRUE表示第一行是表头,否则(FALSE)文件第一行作为数据处理;
载入文件:
单击“载入”;或者>load(" myworkspace. RData")
2.2数字、字符与向量
1.赋值:>x<-c(...........);
assign("x",c(....));
>y<-c(x,0,x) 定义变量y有11个分量,其中两边是变量x,中间是0
2.运算
加+,减-,乘*,除/,乘方^
%/% 表示整数除法,例 5%/%3=1;%%表示求余数,例5%%3=2
基本初等函数:log, exp, cos, tan, sqrt
函数:min(), max(), range()
which.min(), which.max()表示第几个是最小/大值
求和:sum(),乘积:prod()
向量个数:length()
中位数:median(); 均值:mean(); 方差var() 标准差 sd()
顺序统计量(递增):sort()
3.序列
等差数列:a:b表示从a 开始,逐项加1(或减1),直到b
x<-1:30表示向量(1,2,。。。30)
等差运算优于乘法运算,1:n-1表示向量1:n-1
等间隔函数: seq(X1, X2, by=X3), 从X1到X2, 间隔X3.不特别注明by,间隔为1
or: s<-seq(length=x1, from=x2, by=x3)
重复函数:s<-rep(x, times=3)
4.逻辑向量
运算符:<, <=, >, >=, ==, !=(不等于)
且"&", 或"|", 非"!"
判断:>all(c(1,2,3,4,5)>3)
[1] FALSE
5.缺失数据NA。。。。待补全

《统计建模与R软件》的笔记-第130页

老师把high-level,low-level翻译成高水平作图和低水平作图未免太差强人意,译成全景作图函数和要素作图函数怎么样?
*事实上,R的所有作图函数分为两类,一类是高层函数(high-level),用以生成新的图形,另一类就是低层函数(low-level),这一类指的正是绘制图形元素的这些基础函数。
———谢益辉《现代统计图形》

《统计建模与R软件》的笔记-第84页 - 2.5.5

5.数组的广义转置> A<-array(1:24,dim=c(2,3,4))
> B<-aperm(A,c(2,3,1))
B[i,j,k]=A[j,k,i]
我觉得不对。应该是A[j,k,i]=B[k,i,j]

《统计建模与R软件》的笔记-第90页 - R软件使用-矩阵

2.5多维数组和矩阵
定义数组:>z<-1:12
>dim(z)<-c(3,4)
>z
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
dim 是各维的长度
多维数组:>x<-array(1:20, dim=c(4,5)) 4×5的二维数组(矩阵)
>x
构造矩阵:>A<matrix(1:15, nrow=3,ncol=5,byrow=TRUE)
>A
3×5的矩阵,byrow=TRUE去掉,按列置放
>A<-matrix(c(1,2,2,3,3,4),nrow=2)
访问某个元素:>a[1,2:3,2:3]表示第一下标为1,第二、三下标为2或3的元素
数组进行四则运算,形状相同;
形状不同:短向量的数据循环使用
转置运算:>t(A)
求行列式:det()
向量内积:x%*%y
crossprod(x,y) 内积运算函数,表交叉乘积
向量外积:>x%o%y; outer(x,y)
乘法:A*B,对应元素乘积(相同维度)
A%*%B 两个矩阵乘积(A的列数等于B的行数)
对角:diag(v),v为向量,表示已v为对角线元素的对角阵
diag(M),M为矩阵,表示对角线上的元素
方程组:Ax=b
x<-solve(A);x
逆矩阵 B<-solve(A);B

《统计建模与R软件》的笔记-第69页 - 2.3.3

可以用attr(object,name)的形式存取对象object的名为name的属性,例如,
> attr(x,"names")
[1] "apple" "orange"
> attr(x,"name")
[1] "apple" "orange"
> attr(x,"nam")
[1] "apple" "orange"
> attr(x,"n")
[1] "apple" "orange"
原因见笔者对另一本书《R语言及Bioconductor在基因组分析中的应用》所做的笔记:
http://book.douban.com/annotation/24010007/
attr {base} R Documentation
Object Attributes
Description
Get or set specific attributes of an object.
Usage
attr(x, which, exact = FALSE)
attr(x, which) <- value
Arguments
x
an object whose attributes are to be accessed.
which
a non-empty character string specifying which attribute is to be accessed.
exact
logical: should which be matched exactly?
value
an object, the new value of the attribute, or NULL to remove the attribute.
这点在R文档中得到了验证。
> attr(x,"abc")<-"test";x
apple grapes
2.5 2.1
attr(,"type")
[1] "fruit"
attr(,"abc")
[1] "test"
> attr(x,"abc")
[1] "test"
> attr(x,"a")
[1] "test"
> attr(x,"abc",exact=TRUE)
[1] "test"
> attr(x,"a",exact=TRUE)
NULL

《统计建模与R软件》的笔记-第五章 假设检验 - 第五章 假设检验

最后一道题5.18
wilcox.test(x,y,paired=TRUE,exact=FALSE) (Wilcox符号秩检验)
p-value = 0.001586 < 0.05,应该拒绝H0
wilcox.test(x,y,paired=FALSE,exact=FALSE) (Wilcox秩和检验)
p-value = 0.05509 >0.05. 应该保留H0
差别怎么这么大?

《统计建模与R软件》的笔记-第63页 - 2.2.5

> labs<-paste("X",1:6,sep="");labs
[1] "X1" "X2" "X3" "X4" "X5" "X6"
> labs1<-paste("X",1:6,sep=" ");labs1
[1] "X 1" "X 2" "X 3" "X 4" "X 5" "X 6"
> labs1<-paste("X",1:6);labs1
[1] "X 1" "X 2" "X 3" "X 4" "X 5" "X 6"
不制定sep的数值,默认为空格。

《统计建模与R软件》的笔记-第192页 - 参数估计

矩法(method of moments)是由英国统计学家K Pearson在20世纪初提出来的,它的中心思想就是用样本矩去估计总体矩。要是不知道moment在力学里是力矩的意思,估计该觉得这词又文艺了一把...

《统计建模与R软件》的笔记-第90页 - 2.7.1

> rt<-read.table("R_YiXue/Chapter02/houses.data")
> rt
Price Floor Area Rooms Age Cent.heat
01 52.00 111 830 5 6.2 no
02 54.75 128 710 5 7.5 no
03 57.50 101 1000 5 4.2 no
04 57.50 131 690 6 8.8 no
05 59.75 93 900 5 1.9 yes
> is.data.frame(rt)
[1] TRUE
> rt2<-read.table("R_YiXue/Chapter02/houses2.data")
> rt2
V1 V2 V3 V4 V5 V6
1 Price Floor Area Rooms Age Cent.heat
2 52.00 111.0 830 5 6.2 no
3 54.75 128.0 710 5 7.5 no
4 57.50 101.0 1000 5 4.2 no
5 57.50 131.0 690 6 8.8 no
6 59.75 93.0 900 5 1.9 yes
> rt2<-read.table("R_YiXue/Chapter02/houses2.data",header=TRUE)
> rt2
Price Floor Area Rooms Age Cent.heat
1 52.00 111 830 5 6.2 no
2 54.75 128 710 5 7.5 no
3 57.50 101 1000 5 4.2 no
4 57.50 131 690 6 8.8 no
5 59.75 93 900 5 1.9 yes
> rt3<-read.table("R_YiXue/Chapter02/houses2.data",skip=1)
Warning message:
In read.table("R_YiXue/Chapter02/houses2.data", skip = 1) :
incomplete final line found by readTableHeader on 'R_YiXue/Chapter02/houses2.data'
> rt4<-read.table("R_YiXue/Chapter02/houses2.data",header=TRUE,skip=1)
Warning message:
In read.table("R_YiXue/Chapter02/houses2.data", header = TRUE, skip = 1) :
incomplete final line found by readTableHeader on 'R_YiXue/Chapter02/houses2.data'
> rt3
V1 V2 V3 V4 V5 V6
1 52.00 111 830 5 6.2 no
2 54.75 128 710 5 7.5 no
3 57.50 101 1000 5 4.2 no
4 57.50 131 690 6 8.8 no
5 59.75 93 900 5 1.9 yes
> rt4
X52.00 X111.0 X830 X5 X6.2 no
1 54.75 128 710 5 7.5 no
2 57.50 101 1000 5 4.2 no
3 57.50 131 690 6 8.8 no
4 59.75 93 900 5 1.9 yes

《统计建模与R软件》的笔记-第61页 - 2.2.2

> seq(2,10)
[1] 2 3 4 5 6 7 8 9 10
> seq(2.10)
[1] 1 2
即若seq()函数中只有一个参数,默认为是to的值。得到从1到此参数数值的间隔为1的序列。

《统计建模与R软件》的笔记-第62页 - 2.2.4

> x<-c(0/1,0/0,1/0,NA);x
[1] 0 NaN Inf NA
> is.na(x)
[1] FALSE TRUE FALSE TRUE
> is.nan(x)
[1] FALSE TRUE FALSE FALSE
> is.finite(x)
[1] TRUE FALSE FALSE FALSE
> is.infinite(x)
[1] FALSE FALSE TRUE FALSE
数据总体来说分为两类,一类是确定数据,一类是不确定数据。后者标记为NaN。
确定数据可以分为两类,一类是缺失数据,一类是非缺失数据。前者标记为NA。不确定数据也可以认为是缺失数据。
非缺失数据又可氛围两类,一类是有限数据,记为finite;一类是无穷数据,记为infinite。

《统计建模与R软件》的笔记-第42页

下载地址:http://cran.r-project.org/bin/windows/base/

《统计建模与R软件》的笔记-第五章 - 第五章

mark一下,终于看完第五章了。
数理统计的底子太弱,看各种检验真是累死人了

《统计建模与R软件》的笔记-第284页 - 回归诊断

异常样本的存在往往会给回归模型带来不稳定,为此,人们必须提出所谓回归诊断的问题(regression diagnostic),其主要内容有:
误差项是否满足独立性、等方差性、正态性
选择线性模型是否合适
是否存在异常样本
回归分析的结果是否对某些样本的依赖过重,即回归模型是否剧本稳定性
自变量之间是否存在高度相关,即是否有多重共线性问题存在

《统计建模与R软件》的笔记-第66页 - 2.2.7

> v[-(1:5)]
[1] 15 16 17 18 19 20
表示扣除相应的元素
> v
[1] 10 11 12 13 14 15 16 17 18 19 20
> v[c(-1,-2,-3,-4,-5)]
[1] 15 16 17 18 19 20
> v[-1]
[1] 11 12 13 14 15 16 17 18 19 20
> v[-2]
[1] 10 12 13 14 15 16 17 18 19 20
> v[-5]
[1] 10 11 12 13 15 16 17 18 19 20
-n为扣除第n个元素

《统计建模与R软件》的笔记-第284页 - "xtable" to make tables for linear regression


 统计建模与R软件下载 更多精彩书评


 

外国儿童文学,篆刻,百科,生物科学,科普,初中通用,育儿亲子,美容护肤PDF图书下载,。 零度图书网 

零度图书网 @ 2024