R 2001 9 R R S Splus R S 1 R 1: R 2 [File] [Exit] 1
q( ) 2: R 2 R R R R C:nProgram FilesnRnrw1030) [File] [Change Dir] c:ndatadir OK 2
2.1 7+3 1 10 7-3 7*3 7/3 7^3 2 > 7+3 [1] 10 > 7-3 [1] 4 > 7*3 [1] 21 > 7/3 [1] 2.333333 > 7^3 [1] 343 > 7*(1+2) [1] 21 > 2*6+2 [1] 14 > 6/3*2 [1] 4 > 2^3*4 [1] 32 R 1 R 2 [1] 3
2.2 2.5 3 10 > 2.5*3 [1] 7.5 > 2.5*4 [1] 10 > 2.5*5 [1] 12.5 > 2.5*6 [1] 15 > 2.5*7 [1] 17.5 > 2.5*8 [1] 20 > 2.5*9 [1] 22.5 > 2.5*10 [1] 25 2.5 > h<-2.5 h 2.5 3 h > h<-2.5 > h*3 [1] 7.5 > h*4 [1] 10 > h*5 3 < ; ; > 4
[1] 12.5 > h*6 [1] 15 2.3 2.3.1 c( ) seq( ) rep( ) 2.5 h h* c( ) 4 > w<-c(3,4,5,6,7,8,9,10) > w [1] 3 4 5 6 7 8 9 10 > h*w [1] 7.5 10.0 12.5 15.0 17.5 20.0 22.5 25.0 w 3 10 h 1 1 100 > w<-c(1,2,3,4,5,...,99,100) seq( ) 1 100 4 c( ) c concatenate 5
> w<-seq(1,100) > w [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 [91] 91 92 93 94 95 96 97 98 99 100 5 [1] [19] 6 > v<-seq(100,110) > v [1] 100 101 102 103 104 105 106 107 108 109 110 > seq(100,110,2) [1] 100 102 104 106 108 110 > seq(1,10)/10 [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 > seq(1,2,length=20) [1] 1.000000 1.052632 1.105263 1.157895 1.210526 1.263158 1.315789 1.368421 [9] 1.421053 1.473684 1.526316 1.578947 1.631579 1.684211 1.736842 1.789474 [17] 1.842105 1.894737 1.947368 2.000000 seq(s,e) s e 1 2 seq(s,e,step) rep( ) rep(2,4) (2,2,2,2) rep( ) > rep(2,4) [1] 2 2 2 2 > rep(c(0,1),3) [1] 0 1 0 1 0 1 > rep(c(-1,0,1),c(1,2,3)) [1] -1 0 0 1 1 1 1 2 3 5 100 1000 1000 6 [1] 6
2.3.2 > w[3] 3 w 3 3 w[3] [ ] > w<-seq(1,100) > w[3] [1] 3 > w[11:20] [1] 11 12 13 14 15 16 17 18 19 20 > w[c(1,3,7,10)] [1] 1 3 7 10 > w[seq(50,60)] [1] 50 51 52 53 54 55 56 57 58 59 60 > w[seq(1,20,2)] [1] 1 3 5 7 9 11 13 15 17 19 w[11:20] 11 20 c( ) seq( ) seq(1,20,2) 1 2 20 3 w>50 [ ] 50 w>50 w>50 TRUE FALSE W 1 50 w>50 50 TRUE FALSE 7
3: TRUE < > <= >= ==!= & and j or > w<-seq(1,10) > w [1] 1 2 3 4 5 6 7 8 9 10 > w[w>=5 & w<=7] [1] 5 6 7 8
> w<-seq(1,10) > w [1] 1 2 3 4 5 6 7 8 9 10 > w[-2] [1] 1 3 4 5 6 7 8 9 10 > w[-c(1,3,5)] [1] 2 4 6 7 8 9 10 > w[-2:9] Error: only 0's may mix with negative subscripts > w[-(2:9)] [1] 1 10 2:9 7 2.3.3 + ; w<-seq(1,10) > w [1] 1 2 3 4 5 6 7 8 9 10 > w+2 [1] 3 4 5 6 7 8 9 10 11 12 > w-5 [1] -4-3 -2-1 0 1 2 3 4 5 7 2 9 1 9
> x<-c(1,2,3,4,5) y<-c(1,3,5,7,9) > x+y [1] 2 5 8 11 14 > x-y [1] 0-1 -2-3 -4 > x*y [1] 1 6 15 28 45 > x/y [1] 1.0000000 0.6666667 0.6000000 0.5714286 0.5555556 1 R 8 1 1 GDP 2 GDP 3 4 1 c( ) matrix( ) matrix(,nrow=,ncol=,byrow=f or T) 1:6 > matrix(1:6,nrow=2) [,1] [,2] [,3] [1,] 1 3 5 8 10
[2,] 2 4 6 > matrix(1:6,nrow=3) [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 9 nrow nrow ncol byrow T F F T > matrix(1:6,nrow=2,byrow=t) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 > matrix(1:6,nrow=3,byrow=t) [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6 10 > A<-matrix(1:6,nrow=3) B<-matrix(c(1,3,5,7,9,11),nrow=3) > A [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > B [,1] [,2] [1,] 1 7 9 10 1 6 byrow 11
[2,] 3 9 [3,] 5 11 > A+B [,1] [,2] [1,] 2 11 [2,] 5 14 [3,] 8 17 > A-B [,1] [,2] [1,] 0-3 [2,] -1-4 [3,] -2-5 > A*B [,1] [,2] [1,] 1 28 [2,] 6 45 [3,] 15 66 > A/B [,1] [,2] [1,] 1.0000000 0.5714286 [2,] 0.6666667 0.5555556 [3,] 0.6000000 0.5454545 R %*% A B > A %*% B Error in A %*% B : non-conformable arguments 11 A B 3 2 AB 0 B t(b) 11 12
> A %*% t(b) [,1] [,2] [,3] [1,] 29 39 49 [2,] 37 51 65 [3,] 45 63 81 > t(a) %*% B [,1] [,2] [1,] 22 58 [2,] 49 139 3 2 2 3 3 3 2 3 3 2 2 2 A c(1,2) 1 1 %*% > x<-c(1,2) > A %*% x [,1] [1,] 9 [2,] 12 [3,] 15 c(1,2,3) A > y<-c(1,2,3) > y %*% A [,1] [,2] [1,] 14 32 A y y 3 1 3 y 0 A R 13
2.4 1990 2000 GDP R gdpcons.txt 12 13 ls( ) > ls() [1] "gdpcons" "last.warning" "year" gdpcons year 14 gdpcons > gdpcons GDP CONS [1,] 312712.7 174382.7 [2,] 321490.5 177074.9 [3,] 331710.7 184799.3 [4,] 339823.8 189292.0 [5,] 353436.2 194237.4 [6,] 368184.1 201627.8 [7,] 379895.7 209050.0 [8,] 399442.3 217356.6 [9,] 424657.3 229129.5 [10,] 445468.8 238784.9 [11,] 469780.5 248840.1 [12,] 481660.7 256905.6 [13,] 483375.6 261560.2 [14,] 485498.4 266385.2 [15,] 490730.7 272342.2 [16,] 502794.3 277906.5 [17,] 520053.8 284766.8 [18,] 521315.1 281393.7 [19,] 518380.7 285094.0 [20,] 525695.8 289454.2 [21,] 530312.8 288981.1 > year [1] 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 [16] 1995 1996 1997 1998 1999 2000 12 gdpcons.txt 13 14 rm( ) 14
gdpcons year 15 dim( ) length( ) > dim(gdpcons) [1] 21 2 > dim(year) NULL > length(year) [1] 21 dim( ) length( ) gdpcons year gdpcons 1 GDP 2 CONS > dimnames(gdpcons) [[1]] NULL [[2]] [1] "GDP" "CONS" dimnames( ) [[1]] [[2]] 16 NULL 1 \GDP" 2 \CONS" gdpcons > dimnames(gdpcons)[[1]]<-year > gdpcons GDP CONS 1980 312712.7 174382.7 1981 321490.5 177074.9 1982 331710.7 184799.3 1983 339823.8 189292.0 15 16 15
1984 353436.2 194237.4 1985 368184.1 201627.8 1986 379895.7 209050.0 1987 399442.3 217356.6 1988 424657.3 229129.5 1989 445468.8 238784.9 1990 469780.5 248840.1 1991 481660.7 256905.6 1992 483375.6 261560.2 1993 485498.4 266385.2 1994 490730.7 272342.2 1995 502794.3 277906.5 1996 520053.8 284766.8 1997 521315.1 281393.7 1998 518380.7 285094.0 1999 525695.8 289454.2 2000 530312.8 288981.1 dimnames( ) 17 gdpcons GDP GDP gdpcons 1 [ ] A[1,1] 1 1 A(1,1) 1 GDP > gdpcons[1,1] [1] 312712.7 > gdpcons[1:3,1] 1980 1981 1982 312712.7 321490.5 331710.7 > gdpcons[,1] 1980 1981 1982 1983 1984 1985 1986 1987 312712.7 321490.5 331710.7 339823.8 353436.2 368184.1 379895.7 399442.3 1988 1989 1990 1991 1992 1993 1994 1995 424657.3 445468.8 469780.5 481660.7 483375.6 485498.4 490730.7 502794.3 1996 1997 1998 1999 2000 520053.8 521315.1 518380.7 525695.8 530312.8 1 1 gdpcons(1,1) 1:3 17 [[ ]] 2.5 16
1 3 1 gdpcons 3 gdpcons[3,] "GDP" "1980" 18 > gdpcons["2000","cons"] [1] 288981.1 > gdpcons["1995",] GDP CONS 502794.3 277906.5 x<-c("a",1,2,3,"b") > x<-c("a",1,2,3,"b") > x [1] "a" "1" "2" "3" "b" GDP "bubble" "b" "a" 1987 1991 1980 1986 "b" 1987 1991 "bubble" "a" id" 18 R 17
> id<-c("b","b","b","b","b","b","b","bubble","bubble","bubble","bubble","bubble", + "a","a","a","a","a","a","a","a","a") > length(id) [1] 21 21 R id gdpcons 3 rbind( ) cbind( ) cbind( ) > cbind(gdpcons,id) gdp cons id 1980 "312712.7" "174382.7" "b" 1981 "321490.5" "177074.9" "b" 1982 "331710.7" "184799.3" "b" 1983 "339823.8" "189292" "b" 1984 "353436.2" "194237.4" "b" 1985 "368184.1" "201627.8" "b" 1986 "379895.7" "209050" "b" 1987 "399442.3" "217356.6" "bubble" 1988 "424657.3" "229129.5" "bubble" 1989 "445468.8" "238784.9" "bubble" 1990 "469780.5" "248840.1" "bubble" 1991 "481660.7" "256905.6" "bubble" 1992 "483375.6" "261560.2" "a" 1993 "485498.4" "266385.2" "a" 1994 "490730.7" "272342.2" "a" 1995 "502794.3" "277906.5" "a" 1996 "520053.8" "284766.8" "a" 1997 "521315.1" "281393.7" "a" 1998 "518380.7" "285094" "a" 1999 "525695.8" "289454.2" "a" 2000 "530312.8" "288981.1" "a" data.frame( ) 18
> gdpcons<-data.frame(gdpcons,id) > gdpcons gdp cons id 1980 312712.7 174382.7 b 1981 321490.5 177074.9 b 1982 331710.7 184799.3 b 1983 339823.8 189292.0 b 1984 353436.2 194237.4 b 1985 368184.1 201627.8 b 1986 379895.7 209050.0 b 1987 399442.3 217356.6 bubble 1988 424657.3 229129.5 bubble 1989 445468.8 238784.9 bubble 1990 469780.5 248840.1 bubble 1991 481660.7 256905.6 bubble 1992 483375.6 261560.2 a 1993 485498.4 266385.2 a 1994 490730.7 272342.2 a 1995 502794.3 277906.5 a 1996 520053.8 284766.8 a 1997 521315.1 281393.7 a 1998 518380.7 285094.0 a 1999 525695.8 289454.2 a 2000 530312.8 288981.1 a data.frame( data.frame( ) gdp > gdpcons[,1] [1] 312712.7 321490.5 331710.7 339823.8 353436.2 368184.1 379895.7 399442.3 [9] 424657.3 445468.8 469780.5 481660.7 483375.6 485498.4 490730.7 502794.3 [17] 520053.8 521315.1 518380.7 525695.8 530312.8 > gdpcons$gdp [1] 312712.7 321490.5 331710.7 339823.8 353436.2 368184.1 379895.7 399442.3 [9] 424657.3 445468.8 469780.5 481660.7 483375.6 485498.4 490730.7 502794.3 [17] 520053.8 521315.1 518380.7 525695.8 530312.8 > attach(gdpcons) > gdp [1] 312712.7 321490.5 331710.7 339823.8 353436.2 368184.1 379895.7 399442.3 [9] 424657.3 445468.8 469780.5 481660.7 483375.6 485498.4 490730.7 502794.3 [17] 520053.8 521315.1 518380.7 525695.8 530312.8 19
attach( ) 19 attach( ) detach( ) detach(gdpcons) attach(gdpcons) id > id [1] "b" "b" "b" "b" "b" "b" "b" "bubble" [9] "bubble" "bubble" "bubble" "bubble" "a" "a" "a" "a" [17] "a" "a" "a" "a" "a" 2.5 R R 4 > list(gdp,cons,id) [[1]] [1] 312712.7 321490.5 331710.7 339823.8 353436.2 368184.1 379895.7 399442.3 [9] 424657.3 445468.8 469780.5 481660.7 483375.6 485498.4 490730.7 502794.3 [17] 520053.8 521315.1 518380.7 525695.8 530312.8 [[2]] [1] 174382.7 177074.9 184799.3 189292.0 194237.4 201627.8 209050.0 217356.6 [9] 229129.5 238784.9 248840.1 256905.6 261560.2 266385.2 272342.2 277906.5 [17] 284766.8 281393.7 285094.0 289454.2 288981.1 19 20
[[3]] [1] "b" "b" "b" "b" "b" "b" "b" "bubble" [9] "bubble" "bubble" "bubble" "bubble" "a" "a" "a" "a" [17] "a" "a" "a" "a" "a" gdp cons id list( ) [[1]] [[2]] [[3]] list(gdp,cons,id)[[2]] cons 3 GDP R 3.1 > attach(gdpcons) > plot(year,gdp) GDP 4 plot( ) X Y plot( ) 4 20 > plot(year,gdp,type="l",main="gdp from 1980 to 2000") 20 pch="." 21
gdp 350000 400000 450000 500000 1980 1985 1990 1995 2000 year 4: GDP type type="l" help(plot) 21 GDP from 1980 to 2000 gdp 350000 400000 450000 500000 1980 1985 1990 1995 2000 year 5: GDP ( ) R Version 1.3.0 Microsoft Word 21 help.start( ) Internet Explorer 22
ps jpeg GDP plot(year,gdp,cons) 6 > plot(year,gdp,main="gdp and Consumption",type="l", + ylim=c(100000,600000)) > lines(year,cons,lty=2) plot( ) GDP lines( ) 2 ylim=c(100000,600000) 100000 600000 GDP 300000 cons cons 170000 300000 GDP lines(year,cons,...) lines( ) lty 22 GDP and Consumption gdp 1 e+05 2 e+05 3 e+05 4 e+05 5 e+05 6 e+05 1980 1985 1990 1995 2000 year 6: GDP 22 GDP CONS 23
3.2 plot( ) GDP gdp ; cons gdp 23 > par(mfrow=c(1,2)) > growth.gdp<-(gdp[2:21]-gdp[1:20])/gdp[2:21]*100 > growth.cons<-(cons[2:21]-cons[1:20])/cons[2:21]*100 > plot(year[2:21],growth.gdp,main="gdp and Consumption Growth", + type="l") > lines(year[2:21],growth.cons,lty=2) > saving.rate<-(gdp-cons)/gdp*100 > plot(year,saving.rate,main="macro Saving Rate", + type="l") 1 2 par( ) GDP 100 1 plot( ) plot( ) lines( ) plot 7 24 23 R 24
GDP and Consumption Growth Macro Saving Rate growth.gdp 0 1 2 3 4 5 6 saving.rate 44.5 45.0 45.5 46.0 46.5 47.0 1985 1990 1995 2000 year[2:21] 1980 1985 1990 1995 2000 year 4 7: GDP GDP GDP 4.1 mean( ) var( ) sqrt( ) 1 var( ) n (n-1)/n > mean(growth.gdp) mean(growth.cons) [1] 2.589886 [1] 2.482524 > n<-length(growth.gdp) 24 1 25
> sqrt(var(growth.gdp)*(n-1)/n) [1] 1.78986 > sqrt(var(growth.cons)*(n-1)/n) [1] 1.494700 GDP GDP 1980 1990 10 1990 > ysub<-seq(1981,2000) > mean(growth.gdp[ysub<=1989]) [1] 3.849082 > mean(growth.gdp[ysub>1989]) [1] 1.559634 > mean(growth.cons[ysub<=1989]) [1] 3.426539 > mean(growth.cons[ysub>1989]) [1] 1.710148 > sqrt(var(growth.gdp[ysub<=1989])) [1] 1.157558 > sqrt(var(growth.gdp[ysub>1989])) [1] 1.655496 > sqrt(var(growth.cons[ysub<=1989])) [1] 1.096810 > sqrt(var(growth.cons[ysub>1989])) [1] 1.430941 var( ) ysub 1980 2000 1981 2000 20 20 20 26
T F 1990 10 80 Volatility 1990 4.2 hist( ) GDP > hist(growth.gdp) 8 Histogram of growth.gdp Frequency 0 1 2 3 4 1 0 1 2 3 4 5 6 growth.gdp 8: GDP 27
4.3 GDP 4.3.1 GDP GDP GDP GDP vs Consumption growth.cons 1 0 1 2 3 4 5 0 1 2 3 4 5 6 growth.gdp 9: GDP GDP GDP 28
C Y C = + Y ;1 H 0 : = 0 vs H 1 : 6= 0 > x<-growth.gdp[1:19] > y<-growth.cons[2:20] > reg1<-lm(y~x) > summary(reg1) Call: lm(formula = y ~ x) Residuals: Min 1Q Median 3Q Max -4.0393-0.2069 0.2463 0.5585 1.6227 Coefficients: Estimate Std. Error t value Pr(> t ) (Intercept) 1.2425 0.5446 2.282 0.0357 * x 0.4815 0.1689 2.850 0.0111 * --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.319 on 17 degrees of freedom Multiple R-Squared: 0.3234, Adjusted R-squared: 0.2836 F-statistic: 8.125 on 1 and 17 DF, p-value: 0.01106 GDP 1 19 GDP gdp[1:19] 25 cons[2:20] 25 29
lm( ) summary( ) lm( ) y ~ x ~ y~x 1 + x 2 + x 3 + GDP 0.4815 T 2.850 5% 26 4.3.2 S Y = + T T T=1,2,3,.. > length(saving.rate) [1] 21 > trend<-seq(1,21) > reg1<-lm(saving.rate~trend) > summary(reg1) Call: lm(formula = saving.rate ~ trend) Residuals: Min 1Q Median 3Q Max 26 GDP R 30
-0.9506-0.6835-0.1488 0.4676 1.7126 Coefficients: Estimate Std. Error t value Pr(> t ) (Intercept) 44.94598 0.35065 128.178 <2e-16 *** trend 0.03382 0.02793 1.211 0.241 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 0.7749 on 19 degrees of freedom Multiple R-Squared: 0.07166, Adjusted R-squared: 0.0228 F-statistic: 1.467 on 1 and 19 DF, p-value: 0.2407 trend 0.034 T 1.211 1980 2000 27 5 gdpcons 1. read.table( ) 1 2 28 gdpcons<-read.table("gdpcons.txt",header=t) 1 29 2. scan( ) scan( ) 27 28 gdpcons 29 read.table( ) 31
> x<-scan() 1: 1 2: 2 3: 5 4: 6 5: 8 6: 9 7: Read 6 items > x [1] 1 2 5 6 8 9 scan() 1 x 30 R write( ) write( ) 31 write(t(x),"filename",ncol=ncol(x)) t(x) x 24 R 30 scan( ) help(scan) 31 R 32
32 R m2y <- function(x){ if((length(x) %% 12)!= 0) return("inconsistent Number of Data") n <- length(x) / 12 y <- 1:n for(i in 1:n){ b <- 12*(i-1)+1 e <- 12*(i-1)+12 y[i] <- mean(x[b:e]) } y } m2y.r R > source("m2y.r") x.month x.year<-m2y(x.month) m2y( ) 33 R R Save workspace image? OK.Rdata.Rdata R 32 33 [File] [Source R code] OK 33
.Rdata.Rdata [File] [Load Workspace] [Save Workspace] [Save Workspace].Rdata.Rdata 34 34.Rdata 34
YEAR GDP CONS 1980 312712.7 174382.7 1981 321490.5 177074.9 1982 331710.7 184799.3 1983 339823.8 189292.0 1984 353436.2 194237.4 1985 368184.1 201627.8 1986 379895.7 209050.0 1987 399442.3 217356.6 1988 424657.3 229129.5 1989 445468.8 238784.9 1990 469780.5 248840.1 1991 481660.7 256905.6 1992 483375.6 261560.2 1993 485498.4 266385.2 1994 490730.7 272342.2 1995 502794.3 277906.5 1996 520053.8 284766.8 1997 521315.1 281393.7 1998 518380.7 285094.0 1999 525695.8 289454.2 2000 530312.8 288981.1 35