07年3月开始,R的创始人Ross Ihaka 在Auckland大学统计系为3年级学生讲授Statistical Computing课程
本贴将学习Ross讲稿
课程地址:http://www.stat.auckland.ac.nz/~stat380/
该课程的软件计算包当然是R,另外还有一个代码编辑工具Tinn-R,个人觉得十分不错。
R的下载地址(要求能上外网):
http://cran.cnr.berkeley.edu/bin/windows/base/R-2.4.1-win32.exe
Tinn-R的下载地址(要求能上外网):
http://www.stat.auckland.ac.nz/~stat380/Tinn-R%201.17.2.4%20setup.exe
[此贴子已经被作者于2007-4-5 10:36:59编辑过]
课程简介:
This course provides an introduction to statistical computing using the R statistical computing environment. The course will provide an introduction to R programming, including:
An introduction to R
Control flow and function writing
Data structures and their use
Numerical methods
Simulation
Object-oriented programming
Graphics programming
The content of this course differs from that of other courses in the Department of Statistics in that it does not emphasise data analysis and interpretation, but instead concentrates on programming.
We will assume that students have used R or some other similar programming environment, but will not assume that students have had extensive prior programming experience.
[此贴子已经被作者于2007-4-5 20:02:44编辑过]
Ross的讲义可以到如下地址下载
http://www.stat.auckland.ac.nz/~stat380/lectures.html
共15部分,如果大家不能上外网,我会上传,下面正是开始
基本结构为:执行讲义中的程序代码,给出输出结果
同时我将上传代码Tinn-R文件,方便大家学习
[此贴子已经被作者于2007-4-5 11:59:52编辑过]
01 Introduction
> ############# 01 Introduction#################
> ### Simple R Expression
> 1+2
[1] 3
> 1/2
[1] 0.5
> 17^2
[1] 289
> 1+2*3
[1] 7
> (1+2)*3
[1] 9
> ### R functions
> sqrt(2)
[1] 1.414214
> log(10)
[1] 2.302585
> log10(10)
[1] 1
> sin(1)
[1] 0.841471
> 4*atan(1)
[1] 3.141593
> ### Assignment
> z=17
> z*(z+23)
[1] 680
> z=18
> z*(z+23)
[1] 738
> ### Numeric Vectors
> x=c(1,2,3,4)
> x
[1] 1 2 3 4
> ### Sequences
> 1:50
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
[23] 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
[45] 45 46 47 48 49 50
> 5:-5
[1] 5 4 3 2 1 0 -1 -2 -3 -4 -5
> ### Combining vectors
> x=c(1,2,3,4)
> c(x,10)
[1] 1 2 3 4 10
> c(x,x)
[1] 1 2 3 4 1 2 3 4
> ### Vector Arithmetic
> x
[1] 1 2 3 4
> 2*x+1
[1] 3 5 7 9
> sqrt(x)
[1] 1.000000 1.414214 1.732051 2.000000
> ### The Recycling Rule
> c(1,2,3,4)+c(1,2)
[1] 2 4 4 6
> ### The Modulo and Integer Division Operators
> 11%%3
[1] 2
> 1:10%%2
[1] 1 0 1 0 1 0 1 0 1 0
> 13.5%%2
[1] 1.5
> 13.5%/%2
[1] 6
> ### Elements and Subsets
> x[3]
[1] 3
> x[c(1,3)]
[1] 1 3
> x[1:3]
[1] 1 2 3
> ### Negative Subscripts
> x[c(-1,-3)]
[1] 2 4
> ### Changing Vector Subsets
> y=1:10
> y[4:6]=0
> y
[1] 1 2 3 0 0 0 7 8 9 10
> ### Special Numerical Values - Infinity
> 1/0
[1] Inf
> -1/0
[1] -Inf
> 1+Inf
[1] Inf
> 1000/Inf
[1] 0
> ### Special Numerical Values - Not a Number
> 0/0
[1] NaN
> Inf-Inf
[1] NaN
> sqrt(-1)
[1] NaN
Warning message:
NaNs produced in: sqrt(-1)
> ### Special Numerical Values - Not a Available
> 1+sin(NA)
[1] NA
> ### Summary Functions - min, max and rang
> max(1:100)
[1] 100
> max(1:100,Inf)
[1] Inf
> range(1:100)
[1] 1 100
> ### Summary Functions - sum and prod
> sum(1:100)
[1] 5050
> prod(1:10)
[1] 3628800
> ### Summary Functions and NA
> min(NA, 100)
[1] NA
> min(10,20,NA,na.rm=T)
[1] 10
> ### Cumulative Summaries
> cumsum(1:10)
[1] 1 3 6 10 15 21 28 36 45 55
> cumprod(1:10)
[1] 1 2 6 24 120 720 5040 40320
[9] 362880 3628800
> cummax(1:10)
[1] 1 2 3 4 5 6 7 8 9 10
> cummin(1:10)
[1] 1 1 1 1 1 1 1 1 1 1
> ### Parallel Summary Functions
> pmin(c(1,10), c(10,2))
[1] 1 2
> pmax(0,c(-1,0,1))
[1] 0 0 1
[此贴子已经被作者于2007-4-5 11:07:50编辑过]
02 vectors
> ##################02 Vectors####################
> ###Logical Vectors
> b=c(T,T,F,F)
> b[1:3]
[1] TRUE TRUE FALSE
> b[1]=F
> b
[1] FALSE TRUE FALSE FALSE
> ###Generating Logical Values
> 3<4
[1] TRUE
> 3>4
[1] FALSE
> c(1,2,3,4)<3
[1] TRUE TRUE FALSE FALSE
> ###Logical Conjuctions
> x=c(1,2,3,4)
> x<2|x>3
[1] TRUE FALSE FALSE TRUE
> all(x>0)
[1] TRUE
> any(x>2)
[1] TRUE
> ###Logical Subsetting
> x=c(1,2,3,4)
> x[x>2]
[1] 3 4
> ###Problem
> x=1:10
> x
[1] 1 2 3 4 5 6 7 8 9 10
> x[x%%2==0]
[1] 2 4 6 8 10
> x[x%%2==1]
[1] 1 3 5 7 9
> x[c(F,T)]
[1] 2 4 6 8 10
> x[c(T,F)]
[1] 1 3 5 7 9
> ###Character Vectors
> s=c("first", "second", "third")
> s[1:2]
[1] "first" "second"
> s[1] = "initial"
> s
[1] "initial" "second" "third"
> ###String Manipulation
> s
[1] "initial" "second" "third"
> nchar(s)
[1] 7 6 5
> substring(s,1,2:3)
[1] "in" "sec" "th"
> ###Pasting Strings Together
> paste("First", "Second", "Third")
[1] "First Second Third"
> paste("First", "Second", "Third", sep=":")
[1] "First:Second:Third"
> paste("First", "Second", "Third", sep="")
[1] "FirstSecondThird"
> ###Pasting Vectors
> paste(s,"element",sep="-")
[1] "initial-element" "second-element" "third-element"
> paste(s,collapse="->")
[1] "initial->second->third"
> ###Complex-Valued Vectors
> z=10+20i
> z
[1] 10+20i
> z=-1+0i
> sqrt(z)
[1] 0+1i
> ###Vector Mode and Length
> mode(1:10)
[1] "numeric"
> length(1:10)
[1] 10
> logical(5)
[1] FALSE FALSE FALSE FALSE FALSE
> numeric(5)
[1] 0 0 0 0 0
> ###Creating Vectors
> vector("numeric",5)
[1] 0 0 0 0 0
> vector("logical",5)
[1] FALSE FALSE FALSE FALSE FALSE
> numeric(0)
numeric(0)
> ###Type Coercion
> c(T,17)
[1] 1 17
> c(T,17,"twelve")
[1] "TRUE" "17" "twelve"
> ###Type Coercion Idioms
> x=1:10
> sum(x>5)
[1] 5
> cumsum(x>5)
[1] 0 0 0 0 0 1 2 3 4 5
> ###Explicit coercion
> "1"+"2"
Error in "1" + "2" : non-numeric argument to binary operator
> as.numeric("1")
[1] 1
> ###Named Vectors
> v=1:4
> names(v)=c("A","B","C","D")
> v
A B C D
1 2 3 4
> names(v)
[1] "A" "B" "C" "D"
> ###Subsetting Using Names
> v=1:4
> names(v)=c("A","B","C","D")
> v["A"]
A
1
> v[c("A","D")]
A D
1 4
> ###List
> lst=list(10,"eleven",T)
> ###Printing Lists
> lst
[[1]]
[1] 10
[[2]]
[1] "eleven"
[[3]]
[1] TRUE
> ###Named Lists
> list(a=10,b="eleven",T)
$a
[1] 10
$b
[1] "eleven"
[[3]]
[1] TRUE
> ###Elements and Subsets of Lists
> lst=list(10,"eleven",T)
> lst[[1]]
[1] 10
> lst[1]
[[1]]
[1] 10
> ###Extracting Named Elements
> lst=list(a=10,b="eleven",T)
> lst[["a"]]
[1] 10
> lst$a
[1] 10
> ###The NULL Object
> length(NULL)
[1] 0
> as.numeric(NULL)
numeric(0)
> as.list(NULL)
list()
[此贴子已经被作者于2007-4-5 11:10:06编辑过]
03 Control Flow
> ################## 03 Control Flow ###############################
> ### Expressions and Compound Expressions
> x={10;20}
> x
[1] 20
> ### Assignments within Compound Expressions
> z={x=10;y=x^2;x+y}
> x
[1] 10
> y
[1] 100
> z
[1] 110
> ### If then Else
> ### For Loop Example 1
> x=1:10
> s=0
> for(i in 1:length(x))
+ s=s+x
> s
[1] 55
> ### For Loop Example 2
> x=rnorm(10)
> x
[1] 0.09689273 -1.03691591 -1.56250044 1.46877663 0.86120905
[6] 0.25595239 -0.34517049 -0.81650957 -1.11443628 -0.27185452
> s=0
> for(i in x)
+ s=s+i
> s
[1] -2.464556
> ### The "Next" Statement
> ### While loop
> ### While loop Example
> n=0
> s=0
> while(s<=100){
+ n=n+1
+ s=s+n
+ }
> c(n,s)
[1] 14 105
> ### Repeat Loops
> ### Exiting from Loops with "Break"
> n=0
> s=0
> repeat{
+ if(s>100)
+ break
+ n=n+1
+ s=s+n
+ }
> c(n,s)
[1] 14 105
> ### Defining Functions
> ### A Simple Example of a Function
> square=function(x) x*x
> square(10)
[1] 100
> square(1:10)
[1] 1 4 9 16 25 36 49 64 81 100
> ### Functions Defined in Terms of Other Functions
> sumsq=function(x) sum(square(x))
> sumsq(1:10)
[1] 385
> sumsq=function(x) sum(square(x-mean(x)))
> sumsq(1:10)
[1] 82.5
> ### Functions in General
> ### Evaluation of Functions
> hypot=function(a,b) sqrt(a^2 + b^2)
> hypot(3,4)
[1] 5
> ### Optional Arguments to Functions
> sumsq=function(x,about=0) sum((x-about)^2)
> ### The Benefits of Default Arguments
> ### Specifying Particular Optional Arguments
> sumsq(1:10,about=mean(1:10))
[1] 82.5
> sumsq(about=mean(1:10),1:10)
[1] 82.5
> ### Argument Matching Rules
> ### Argument Matching Examples
> sumsq(1:10,mean(1:10))
[1] 82.5
> sumsq(1:10,about=mean(1:10))
[1] 82.5
> sumsq(1:10,a=mean(1:10))
[1] 82.5
> sumsq(x=1:10,mean(1:10))
[1] 82.5
> sumsq(mean(1:10),x=1:10)
[1] 82.5
>
[此贴子已经被作者于2007-4-5 11:11:41编辑过]
04 Functions
> ################## 04 Functions ###############################
> ### Computationsal Methods
> ### Computing Factorials by Iteration
> factorial = function(n) {
+ f = 1
+ for(i in 1:n)
+ f = f * i
+ f
+ }
> factorial(10)
[1] 3628800
> ### Computing Factorials by Recursion
> factorial =
+ function(n)
+ if (n==1) 1 else n * factorial(n-1)
> factorial(10)
[1] 3628800
> factorial(1000)
[1] Inf
> ### Computing Factorials by Vector Arithmetic
> factorial = function(n) prod(1:n)
> factorial(10)
[1] 3628800
> ### The Gamma Function
> ### Using F(x) to Compute Factorial
> factorial =
+ function(n) gamma(n+1)
> factorial(1:10)
[1] 1 2 6 24 120 720 5040 40320
[9] 362880 3628800
> ### Computing Binomial Probabilities
> bincoef =
+ function(n, k)
+ factorial(n)/(factorial(k) * factorial(n-k))
> binprob =
+ function(n, p)
+ bincoef(n, 0:n) * p^(0:n) * (1-p)^(n:0)
> round(binprob(10, .25), 4)
[1] 0.0563 0.1877 0.2816 0.2503 0.1460 0.0584 0.0162 0.0031 0.0004
[10] 0.0000 0.0000
> ### Cumulative Binomial probabilities
> bincum =
+ function(n, p)
+ cumsum(binprob(n, p))
> ### Factorials and Binomial Coefficients
> factorial = function(n) gamma(n + 1)
> choose =
+ function(n, k)
+ factorial(n)/(factorial(k)*factorial(n-k))
> ### Binomial Coefficient Example
> choose(19,3) * choose(23, 3)
[1] 1716099
> ### The Binomial Distribution
> ### Individual Binomial Probabilities
> # The chance of k successes in n trials
> # with chance of success p.
> binp =
+ function(k, n, p)
+ choose(n, k) * p^k * (1 - p)^(n - k)
> ### Binomial Probability Example
> binp(45:55, 100, 1/2)
[1] 0.04847430 0.05795840 0.06659050 0.07352701 0.07802866
[6] 0.07958924 0.07802866 0.07352701 0.06659050 0.05795840
[11] 0.04847430
> sum(binp(45:55, 100, 1/2))
[1] 0.728747
> ### Binomial Probability Example Continued
> sum(binp(40:60, 100, 1/2))
[1] 0.9647998
> sum(binp(35:65, 100, 1/2))
[1] 0.99821
> ### A Coin Tossing Experiment
> runif(10) < .5
[1] FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
> sum(runif(100) < .5)
[1] 53
> ### Coin Tossing Continued
> nheads = numeric(10000)
> for(i in 1:10000)
+ nheads = sum(runif(100) < .5)
> sum(45 <= nheads & nheads <= 55)/10000
[1] 0.728
> sum(40 <= nheads & nheads <= 60)/10000
[1] 0.9652
> sum(35 <= nheads & nheads <= 65)/10000
[1] 0.9979
> ### Packaging the Experiment
> cointoss =
+ function(ntosses, pheads, nreps = 1) {
+ nheads = numeric(nreps)
+ for(i in 1:nreps)
+ nheads = sum(runif(100) < pheads)
+ nheads
+ }
> ### Running the Packaged Experiment
> x = cointoss(100, .5, 10000)
> sum(45 <= x & x <= 55)/length(x)
[1] 0.7249
> ### Statistical Computations
> ### Implementing the t Test in R
> y1=rnorm(20)
> y2=rt(30,1)
> n1 = length(y1)
> m1 = mean(y1)
> s1 = sd(y1)
> n2 = length(y2)
> m2 = mean(y2)
> s2 = sd(y2)
> ### Computing the t Statistic
> sp2 = ((n1 - 1) * s1^2 + (n2 - 1) * s2^2) / (n1 + n2 - 2)
> tval = (m1 - m2) / sqrt((1/n1 + 1/n2) * sp2)
> pval = 2 * pt(- abs(tval), n1 + n2 - 2)
> ### The Complete R Function
> ttest =
+ function(y1, y2)
+ {
+ n1 = length(y1); m1 = mean(y1); s1 = sd(y1)
+ n2 = length(y2); m2 = mean(y2); s2 = sd(y2)
+ sp2 = ((n1 - 1) * s1^2 + (n2 - 1) * s2^2) / (n1 + n2 - 2)
+ tval = (m1 - m2) / sqrt((1/n1 + 1/n2) * sp2)
+ pval = 2 * pt(- abs(tval), n1 + n2 - 2)
+ list(t = tval, df = n1 + n2 - 2, pval = pval)
+ }
[此贴子已经被作者于2007-4-5 15:13:24编辑过]
05 Computation
> ################### 05 Computation ###########################
> ### Statistical Computations
> ### Key Quantities in the Computation
> ### Implementing the t Test in R
> diff.means =
+ function(y1, y2) mean(y1) - mean(y2)
> pooled.se =
+ function(y1, y2, n1, n2)
+ sqrt((((n1 - 1) * var(y1) +
+ (n2 - 1) * var(y2)) /
+ (n1 + n2 - 2)) * (1/n1 + 1/n2))
> ### Computing the t-Statistic
> tval = diff.means(y1, y2) /
+ pooled.se(y1, y2, length(y1), length(y2))
> ### Computing p Values
> pval = 2 * pt(- abs(tval), n1 + n2 - 2)
> ### The Complete R Function
> ttest =
+ function(y1, y2)
+ {
+ tval = diff.means(y1, y2) /
+ pooled.se(y1, y2, length(y1), length(y2))
+ pval = 2 * pt(- abs(tval), n1 + n2 - 2)
+ list(t = tval, df = n1 + n2 - 2, pval = pval)
+ }
> ### Using The t-Test Function
> y1 = rnorm(10)
> y2 = rnorm(10) + 2
> ttest(y1, y2)
$t
[1] -8.577713
$df
[1] 48
$pval
[1] 2.991950e-11
> ### Rational Approximations
> ### Continued Fractions
> ### Continued Fractions (Cont. . . )
> ### Problem Analysis
> ### Deriving The Continued Fraction
> cf.expand =
+ function(x, n = 5) {
+ cf = numeric(n)
+ for(i in 1:n) {
+ cf = round(x)
+ delta = x - cf
+ x = 1/delta
+ }
+ cf
+ }
> cf.expand(pi, 7)
[1] 3 7 16 -294 3 -4 5
> ### Producing the Rational Approximation
> rat.approx =
+ function(cf) {
+ n = length(cf)
+ num = cf[n]
+ den = 1
+ if (n > 1)
+ for(j in (n - 1):1) {
+ tmp = num
+ num = cf[j] * tmp + den
+ den = tmp
+ }
+ if (den > 0) c(num, den)
+ else c(-num, -den)
+ }
> ### Examples
> rat.approx(cf.expand(pi, 1))
[1] 3 1
> rat.approx(cf.expand(pi, 2))
[1] 22 7
> rat.approx(cf.expand(pi, 3))
[1] 355 113
> print(pi, digits = 15)
[1] 3.14159265358979
> print(22/7, digits = 15)
[1] 3.14285714285714
> print(355/113, digits = 15)
[1] 3.14159292035398
> ### Sample Sizes from Percentages
> ### Problem Analysis
> ### Algorithm
> ### The Algorithm as an R Function
> find.denom =
+ function(f, places) {
+ eps = .5 * 10^-places
+ n = 1
+ repeat {
+ i = round(n * f)
+ if (all(n * (f - eps) <= i &
+ i <= n * (f + eps)))
+ break
+ n = n + 1
+ }
+ n
+ }
> ### Results
> find.denom(c(.146, .122, .073), 3)
[1] 41
> round(41 * c(.146, .122, .073))
[1] 6 5 3
> ### Determining the Number of Digits
> 1/3
[1] 0.3333333
> round(1/3, 4)
[1] 0.3333
> x = .146
> round(x, 2) == x
[1] FALSE
> round(x, 3) == x
[1] TRUE
> eps = 1e-7
> abs(x - round(x, 2)) < eps
[1] FALSE
> abs(x - round(x, 3)) < eps
[1] TRUE
> ### An R Function
> places =
+ function(f, eps = 1e-7) {
+ places = 0
+ repeat {
+ if (all(abs(f - round(f, places)) < eps))
+ break
+ places = places + 1
+ }
+ places
+ }
> places(.146)
[1] 3
> ### An Improved find.denom Function
> find.denom =
+ function(f) {
+ eps = .5 * 10^-places(f)
+ n = 1
+ repeat {
+ i = round(n * f)
+ if (all((f - eps) * n <= i & (f + eps) * n >= i))
+ break
+ n = n + 1
+ }
+ n
+ }
> ### Improving Performance
> n = floor(1/min(f) + eps)
Error in min(f) : object "f" not found
> floor(1/.073 + .0005)
[1] 13
>
>
[此贴子已经被作者于2007-4-5 16:06:57编辑过]
要学好统计学,需要一种精神。这种精神就是——统计原理。
利用软件包自己写程序就是我们对原理的程序。
R也是一种精神,一种集体协作的精神。希望大家能从本版学到知识,作出更好的成绩!
也感谢众多人的奉献精神。
要学好统计学,需要一种精神。这种精神就是——统计原理。
利用软件包自己写程序就是我们对原理的程序。
R也是一种精神,一种集体协作的精神。希望大家能从本版学到知识,作出更好的成绩!
也感谢众多人的奉献精神。
说的好,真心期待大家多谈谈学习心得,帮助新手,讨论问题,共同进步
06 Matrices
> ################## 06 Matrices ###########################
> ### Matrices
> ### Creating Matrices
> matrix(1:6, nrow = 3, ncol = 2)
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> matrix(1:6, nrow = 3, ncol = 2, byrow = TRUE)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
> ### Matrix Elements and Recycling
> matrix(1, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
> ### Optional Dimension Specifications
> matrix(1:6, nrow = 3)
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> ### Determining Matrix Dimensions
> x = matrix(1:6, nc = 2)
> x
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> nrow(x)
[1] 3
> ncol(x)
[1] 2
> dim(x)
[1] 3 2
> ### Creating Matrices from Rows and Columns
> cbind(1:3, 4:6)
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> rbind(1:3, 4:6)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
> ### Binding Rows and Columns; Recycling
> rbind(1:2, 1:3)
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 1 2 3
Warning message:
number of columns of result
is not a multiple of vector length (arg 1) in: rbind(1, 1:2, 1:3)
> ### Matrices and Naming
> x = matrix(1:6, nrow = 2)
> dimnames(x) = list(c("First", "Second"), c("A", "B", "C"))
> x
A B C
First 1 3 5
Second 2 4 6
> ### Extracting Names
> dimnames(x)
[[1]]
[1] "First" "Second"
[[2]]
[1] "A" "B" "C"
> rownames(x)
[1] "First" "Second"
> colnames(x)
[1] "A" "B" "C"
> ### Extracting Matrix Elements
> s = 0
> x=matrix(1:12,3,4)
> for(i in 1:nrow(x))
+ for(j in 1:ncol(x))
+ s = s + x[i, j]
> s
[1] 78
> s = sum(x)
> s
[1] 78
> ### Matrix Subsets
> x = matrix(1:12, nrow = 3, ncol = 4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> x[1:2, c(2, 4)]
[,1] [,2]
[1,] 4 10
[2,] 5 11
> ### Assigning to Matrix Subsets
> x[1:2, c(2, 4)] = 21:24
> x
[,1] [,2] [,3] [,4]
[1,] 1 21 7 23
[2,] 2 22 8 24
[3,] 3 6 9 12
> x[2:1, c(2, 4)] = 21:24
> x
[,1] [,2] [,3] [,4]
[1,] 1 22 7 24
[2,] 2 21 8 23
[3,] 3 6 9 12
> ### Specifying Entire Rows and Columns
> x = matrix(1:12, nrow = 3, ncol = 4)
> x[1,] = 100
> x
[,1] [,2] [,3] [,4]
[1,] 100 100 100 100
[2,] 2 5 8 11
[3,] 3 6 9 12
> ### Subsetting Matrices as Vectors
> x = matrix(1:6, nrow = 2, ncol = 3)
> x[7]
[1] NA
> row(x)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 2 2
> col(x)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
> x[row(x) < col(x)] = 0
> x
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 2 4 0
> ### An Example
> x = matrix(0, nrow = 4, ncol = 4)
> x[row(x) == col(x)] = 2
> x[abs(row(x) - col(x)) == 1] = 1
> x
[,1] [,2] [,3] [,4]
[1,] 2 1 0 0
[2,] 1 2 1 0
[3,] 0 1 2 1
[4,] 0 0 1 2
> ### Simple Computations with Matrices
> x = matrix(1:4, nrow = 2, ncol = 2)
> x
[,1] [,2]
[1,] 1 3
[2,] 2 4
> x + x^2
[,1] [,2]
[1,] 2 12
[2,] 6 20
> ### Combining Vectors and Matrices
> x + 1:3
[,1] [,2]
[1,] 2 6
[2,] 4 5
Warning message:
longer object length
is not a multiple of shorter object length in: x + 1:3
> ### Row and Column Summaries
> x=matrix(1:12, 3, 4)
> rm = numeric(nrow(x))
> for(i in 1:nrow(x))
+ rm = mean(x[i,])
> rm
[1] 5.5 6.5 7.5
> ### The “Apply” Mechanism
> ### Computing Row and Column Means
> apply(x, 1, mean)
[1] 5.5 6.5 7.5
> apply(x, 2, mean)
[1] 2 5 8 11
> apply(x, 1, sd)
[1] 3.872983 3.872983 3.872983
> ### Apply and Anonymous Functions
> apply(x, 2, function(x) sum(x^2))
[1] 14 77 194 365
> apply(x, 2, function(x) sum((x - mean(x))^2))
[1] 2 2 2 2
> ### More Complex Summaries
> apply(x, 1, range)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 10 11 12
> apply(x, 2, range)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 3 6 9 12
> ### Additional Arguments to Summaries
> ### Sweeping Out Summaries
> ### Other Forms of Sweeping
> x = cbind(1:3, 9:11)
> x
[,1] [,2]
[1,] 1 9
[2,] 2 10
[3,] 3 11
> sweep(x, 2, apply(x, 2, mean), "/")
[,1] [,2]
[1,] 0.5 0.9
[2,] 1.0 1.0
[3,] 1.5 1.1
> ### Matrix Transposes
> x = cbind(1:3, 11:13)
> x
[,1] [,2]
[1,] 1 11
[2,] 2 12
[3,] 3 13
> t(x)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 11 12 13
> ### Matrix Products
> ### (Generalized) Outer Products
> ### A Graphics Example
> x = seq(-1, 1, length = 25)
> y = x
> x
[1] -1.00000000 -0.91666667 -0.83333333 -0.75000000 -0.66666667 -0.58333333
[7] -0.50000000 -0.41666667 -0.33333333 -0.25000000 -0.16666667 -0.08333333
[13] 0.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
[19] 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
[25] 1.00000000
> z = outer(x, y, function(u, v) u^2-v^2)
> z
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0000000 1.597222e-01 3.055556e-01 0.4375000 5.555556e-01
[2,] -0.1597222 0.000000e+00 1.458333e-01 0.2777778 3.958333e-01
[3,] -0.3055556 -1.458333e-01 0.000000e+00 0.1319444 2.500000e-01
[4,] -0.4375000 -2.777778e-01 -1.319444e-01 0.0000000 1.180556e-01
[5,] -0.5555556 -3.958333e-01 -2.500000e-01 -0.1180556 0.000000e+00
[6,] -0.6597222 -5.000000e-01 -3.541667e-01 -0.2222222 -1.041667e-01
[7,] -0.7500000 -5.902778e-01 -4.444444e-01 -0.3125000 -1.944444e-01
[8,] -0.8263889 -6.666667e-01 -5.208333e-01 -0.3888889 -2.708333e-01
[9,] -0.8888889 -7.291667e-01 -5.833333e-01 -0.4513889 -3.333333e-01
[10,] -0.9375000 -7.777778e-01 -6.319444e-01 -0.5000000 -3.819444e-01
[11,] -0.9722222 -8.125000e-01 -6.666667e-01 -0.5347222 -4.166667e-01
[12,] -0.9930556 -8.333333e-01 -6.875000e-01 -0.5555556 -4.375000e-01
[13,] -1.0000000 -8.402778e-01 -6.944444e-01 -0.5625000 -4.444444e-01
[14,] -0.9930556 -8.333333e-01 -6.875000e-01 -0.5555556 -4.375000e-01
[15,] -0.9722222 -8.125000e-01 -6.666667e-01 -0.5347222 -4.166667e-01
[16,] -0.9375000 -7.777778e-01 -6.319444e-01 -0.5000000 -3.819444e-01
[17,] -0.8888889 -7.291667e-01 -5.833333e-01 -0.4513889 -3.333333e-01
[18,] -0.8263889 -6.666667e-01 -5.208333e-01 -0.3888889 -2.708333e-01
[19,] -0.7500000 -5.902778e-01 -4.444444e-01 -0.3125000 -1.944444e-01
[20,] -0.6597222 -5.000000e-01 -3.541667e-01 -0.2222222 -1.041667e-01
[21,] -0.5555556 -3.958333e-01 -2.500000e-01 -0.1180556 -2.775558e-16
[22,] -0.4375000 -2.777778e-01 -1.319444e-01 0.0000000 1.180556e-01
[23,] -0.3055556 -1.458333e-01 -2.220446e-16 0.1319444 2.500000e-01
[24,] -0.1597222 -2.220446e-16 1.458333e-01 0.2777778 3.958333e-01
[25,] 0.0000000 1.597222e-01 3.055556e-01 0.4375000 5.555556e-01
[,6] [,7] [,8] [,9] [,10]
[1,] 6.597222e-01 0.75000000 8.263889e-01 8.888889e-01 0.93750000
[2,] 5.000000e-01 0.59027778 6.666667e-01 7.291667e-01 0.77777778
[3,] 3.541667e-01 0.44444444 5.208333e-01 5.833333e-01 0.63194444
[4,] 2.222222e-01 0.31250000 3.888889e-01 4.513889e-01 0.50000000
[5,] 1.041667e-01 0.19444444 2.708333e-01 3.333333e-01 0.38194444
[6,] 0.000000e+00 0.09027778 1.666667e-01 2.291667e-01 0.27777778
[7,] -9.027778e-02 0.00000000 7.638889e-02 1.388889e-01 0.18750000
[8,] -1.666667e-01 -0.07638889 0.000000e+00 6.250000e-02 0.11111111
[9,] -2.291667e-01 -0.13888889 -6.250000e-02 0.000000e+00 0.04861111
[10,] -2.777778e-01 -0.18750000 -1.111111e-01 -4.861111e-02 0.00000000
[11,] -3.125000e-01 -0.22222222 -1.458333e-01 -8.333333e-02 -0.03472222
[12,] -3.333333e-01 -0.24305556 -1.666667e-01 -1.041667e-01 -0.05555556
[13,] -3.402778e-01 -0.25000000 -1.736111e-01 -1.111111e-01 -0.06250000
[14,] -3.333333e-01 -0.24305556 -1.666667e-01 -1.041667e-01 -0.05555556
[15,] -3.125000e-01 -0.22222222 -1.458333e-01 -8.333333e-02 -0.03472222
[16,] -2.777778e-01 -0.18750000 -1.111111e-01 -4.861111e-02 0.00000000
[17,] -2.291667e-01 -0.13888889 -6.250000e-02 -6.938894e-17 0.04861111
[18,] -1.666667e-01 -0.07638889 -1.665335e-16 6.250000e-02 0.11111111
[19,] -9.027778e-02 0.00000000 7.638889e-02 1.388889e-01 0.18750000
[20,] -1.665335e-16 0.09027778 1.666667e-01 2.291667e-01 0.27777778
[21,] 1.041667e-01 0.19444444 2.708333e-01 3.333333e-01 0.38194444
[22,] 2.222222e-01 0.31250000 3.888889e-01 4.513889e-01 0.50000000
[23,] 3.541667e-01 0.44444444 5.208333e-01 5.833333e-01 0.63194444
[24,] 5.000000e-01 0.59027778 6.666667e-01 7.291667e-01 0.77777778
[25,] 6.597222e-01 0.75000000 8.263889e-01 8.888889e-01 0.93750000
[,11] [,12] [,13] [,14] [,15]
[1,] 9.722222e-01 9.930556e-01 1.000000000 9.930556e-01 9.722222e-01
[2,] 8.125000e-01 8.333333e-01 0.840277778 8.333333e-01 8.125000e-01
[3,] 6.666667e-01 6.875000e-01 0.694444444 6.875000e-01 6.666667e-01
[4,] 5.347222e-01 5.555556e-01 0.562500000 5.555556e-01 5.347222e-01
[5,] 4.166667e-01 4.375000e-01 0.444444444 4.375000e-01 4.166667e-01
[6,] 3.125000e-01 3.333333e-01 0.340277778 3.333333e-01 3.125000e-01
[7,] 2.222222e-01 2.430556e-01 0.250000000 2.430556e-01 2.222222e-01
[8,] 1.458333e-01 1.666667e-01 0.173611111 1.666667e-01 1.458333e-01
[9,] 8.333333e-02 1.041667e-01 0.111111111 1.041667e-01 8.333333e-02
[10,] 3.472222e-02 5.555556e-02 0.062500000 5.555556e-02 3.472222e-02
[11,] 0.000000e+00 2.083333e-02 0.027777778 2.083333e-02 7.632783e-17
[12,] -2.083333e-02 0.000000e+00 0.006944444 1.908196e-17 -2.083333e-02
[13,] -2.777778e-02 -6.944444e-03 0.000000000 -6.944444e-03 -2.777778e-02
[14,] -2.083333e-02 -1.908196e-17 0.006944444 0.000000e+00 -2.083333e-02
[15,] -7.632783e-17 2.083333e-02 0.027777778 2.083333e-02 0.000000e+00
[16,] 3.472222e-02 5.555556e-02 0.062500000 5.555556e-02 3.472222e-02
[17,] 8.333333e-02 1.041667e-01 0.111111111 1.041667e-01 8.333333e-02
[18,] 1.458333e-01 1.666667e-01 0.173611111 1.666667e-01 1.458333e-01
[19,] 2.222222e-01 2.430556e-01 0.250000000 2.430556e-01 2.222222e-01
[20,] 3.125000e-01 3.333333e-01 0.340277778 3.333333e-01 3.125000e-01
[21,] 4.166667e-01 4.375000e-01 0.444444444 4.375000e-01 4.166667e-01
[22,] 5.347222e-01 5.555556e-01 0.562500000 5.555556e-01 5.347222e-01
[23,] 6.666667e-01 6.875000e-01 0.694444444 6.875000e-01 6.666667e-01
[24,] 8.125000e-01 8.333333e-01 0.840277778 8.333333e-01 8.125000e-01
[25,] 9.722222e-01 9.930556e-01 1.000000000 9.930556e-01 9.722222e-01
[,16] [,17] [,18] [,19] [,20]
[1,] 0.93750000 8.888889e-01 8.263889e-01 0.75000000 6.597222e-01
[2,] 0.77777778 7.291667e-01 6.666667e-01 0.59027778 5.000000e-01
[3,] 0.63194444 5.833333e-01 5.208333e-01 0.44444444 3.541667e-01
[4,] 0.50000000 4.513889e-01 3.888889e-01 0.31250000 2.222222e-01
[5,] 0.38194444 3.333333e-01 2.708333e-01 0.19444444 1.041667e-01
[6,] 0.27777778 2.291667e-01 1.666667e-01 0.09027778 1.665335e-16
[7,] 0.18750000 1.388889e-01 7.638889e-02 0.00000000 -9.027778e-02
[8,] 0.11111111 6.250000e-02 1.665335e-16 -0.07638889 -1.666667e-01
[9,] 0.04861111 6.938894e-17 -6.250000e-02 -0.13888889 -2.291667e-01
[10,] 0.00000000 -4.861111e-02 -1.111111e-01 -0.18750000 -2.777778e-01
[11,] -0.03472222 -8.333333e-02 -1.458333e-01 -0.22222222 -3.125000e-01
[12,] -0.05555556 -1.041667e-01 -1.666667e-01 -0.24305556 -3.333333e-01
[13,] -0.06250000 -1.111111e-01 -1.736111e-01 -0.25000000 -3.402778e-01
[14,] -0.05555556 -1.041667e-01 -1.666667e-01 -0.24305556 -3.333333e-01
[15,] -0.03472222 -8.333333e-02 -1.458333e-01 -0.22222222 -3.125000e-01
[16,] 0.00000000 -4.861111e-02 -1.111111e-01 -0.18750000 -2.777778e-01
[17,] 0.04861111 0.000000e+00 -6.250000e-02 -0.13888889 -2.291667e-01
[18,] 0.11111111 6.250000e-02 0.000000e+00 -0.07638889 -1.666667e-01
[19,] 0.18750000 1.388889e-01 7.638889e-02 0.00000000 -9.027778e-02
[20,] 0.27777778 2.291667e-01 1.666667e-01 0.09027778 0.000000e+00
[21,] 0.38194444 3.333333e-01 2.708333e-01 0.19444444 1.041667e-01
[22,] 0.50000000 4.513889e-01 3.888889e-01 0.31250000 2.222222e-01
[23,] 0.63194444 5.833333e-01 5.208333e-01 0.44444444 3.541667e-01
[24,] 0.77777778 7.291667e-01 6.666667e-01 0.59027778 5.000000e-01
[25,] 0.93750000 8.888889e-01 8.263889e-01 0.75000000 6.597222e-01
[,21] [,22] [,23] [,24] [,25]
[1,] 5.555556e-01 0.4375000 3.055556e-01 1.597222e-01 0.0000000
[2,] 3.958333e-01 0.2777778 1.458333e-01 2.220446e-16 -0.1597222
[3,] 2.500000e-01 0.1319444 2.220446e-16 -1.458333e-01 -0.3055556
[4,] 1.180556e-01 0.0000000 -1.319444e-01 -2.777778e-01 -0.4375000
[5,] 2.775558e-16 -0.1180556 -2.500000e-01 -3.958333e-01 -0.5555556
[6,] -1.041667e-01 -0.2222222 -3.541667e-01 -5.000000e-01 -0.6597222
[7,] -1.944444e-01 -0.3125000 -4.444444e-01 -5.902778e-01 -0.7500000
[8,] -2.708333e-01 -0.3888889 -5.208333e-01 -6.666667e-01 -0.8263889
[9,] -3.333333e-01 -0.4513889 -5.833333e-01 -7.291667e-01 -0.8888889
[10,] -3.819444e-01 -0.5000000 -6.319444e-01 -7.777778e-01 -0.9375000
[11,] -4.166667e-01 -0.5347222 -6.666667e-01 -8.125000e-01 -0.9722222
[12,] -4.375000e-01 -0.5555556 -6.875000e-01 -8.333333e-01 -0.9930556
[13,] -4.444444e-01 -0.5625000 -6.944444e-01 -8.402778e-01 -1.0000000
[14,] -4.375000e-01 -0.5555556 -6.875000e-01 -8.333333e-01 -0.9930556
[15,] -4.166667e-01 -0.5347222 -6.666667e-01 -8.125000e-01 -0.9722222
[16,] -3.819444e-01 -0.5000000 -6.319444e-01 -7.777778e-01 -0.9375000
[17,] -3.333333e-01 -0.4513889 -5.833333e-01 -7.291667e-01 -0.8888889
[18,] -2.708333e-01 -0.3888889 -5.208333e-01 -6.666667e-01 -0.8263889
[19,] -1.944444e-01 -0.3125000 -4.444444e-01 -5.902778e-01 -0.7500000
[20,] -1.041667e-01 -0.2222222 -3.541667e-01 -5.000000e-01 -0.6597222
[21,] 0.000000e+00 -0.1180556 -2.500000e-01 -3.958333e-01 -0.5555556
[22,] 1.180556e-01 0.0000000 -1.319444e-01 -2.777778e-01 -0.4375000
[23,] 2.500000e-01 0.1319444 0.000000e+00 -1.458333e-01 -0.3055556
[24,] 3.958333e-01 0.2777778 1.458333e-01 0.000000e+00 -0.1597222
[25,] 5.555556e-01 0.4375000 3.055556e-01 1.597222e-01 0.0000000
> persp(x, y, z, theta = 30, phi = 10)
[此贴子已经被作者于2007-4-6 8:33:48编辑过]
扫码加好友,拉您进群



收藏
