Escolar Documentos
Profissional Documentos
Cultura Documentos
> tobin$durable
[1] 0.0 0.7 0.0 0.0 0.0 0.0 0.0 3.7 0.0 3.0 10.4 0.0 0.0 0.0 1.5
0.0 0.0 3.5 6.1 0.0
> attach(tobin)
> durable
[1] 0.0 0.7 0.0 0.0 0.0 0.0 0.0 3.7 0.0 3.0 10.4 0.0 0.0 0.0 1.5
0.0 0.0 3.5 6.1 0.0
>
>
> # after attach, directly use durable
>
>
>
>
> colnames(tobin)
[1] "durable" "age"
"quant"
> age
[1] 57.7 50.9 48.5 41.7 47.7 59.8 44.3 45.1 51.7 50.0 46.8 58.0 58.9 40.0 34.1
39.9 33.4 48.1 46.6 53.1
> quant
[1] 236 283 207 220 238 216 284 221 275 269 207 249 246 277 231 219 240 266 214
251
> detach
function (name, pos = 2L, unload = FALSE, character.only = FALSE,
force = FALSE)
{
if (!missing(name)) {
if (!character.only)
name <- substitute(name)
pos <- if (is.numeric(name))
name
else {
if (!is.character(name))
name <- deparse(name)
match(name, search())
}
if (is.na(pos))
stop("invalid 'name' argument")
}
packageName <- search()[[pos]]
if (!grepl("^package:", packageName))
return(invisible(.Internal(detach(pos))))
pkgname <- sub("^package:", "", packageName)
for (pkg in search()[-1L]) {
if (grepl("^package:", pkg) && exists(".Depends", pkg,
inherits = FALSE) && pkgname %in% get(".Depends",
pkg, inherits = FALSE))
if (force)
warning(gettextf("package %s is required by %s, which may no lon
ger work correctly",
sQuote(pkgname), sQuote(sub("^package:", "",
pkg))), call. = FALSE, domain = NA)
else stop(gettextf("package %s is required by %s so will not be deta
ched",
sQuote(pkgname), sQuote(sub("^package:", "",
pkg))), call. = FALSE, domain = NA)
}
env <- as.environment(pos)
libpath <- attr(env, "path")
hook <- getHook(packageEvent(pkgname, "detach"))
>
>
> ?subet
No documentation for 'subet' in specified packages and libraries:
you could try '??subet'
> ?subset
starting httpd help server ... done
> subset(tobin,tobin$age<40)
durable age quant
15
1.5 34.1 231
16
0.0 39.9 219
17
0.0 33.4 240
>
> ind<-grep(T,tobin$age<40)
> tobin[ind,]
durable age quant
15
1.5 34.1 231
16
0.0 39.9 219
17
0.0 33.4 240
> tobin(tobin$age<40)
Error: could not find function "tobin"
> tobin[tobin$age<40]
Error in `[.data.frame`(tobin, tobin$age < 40) :
undefined columns selected
> transform(tobin,log.quant=log(tobin$age))
durable age quant log.quant
1
0.0 57.7 236 4.055257
2
0.7 50.9 283 3.929863
3
0.0 48.5 207 3.881564
4
0.0 41.7 220 3.730501
5
0.0 47.7 238 3.864931
6
0.0 59.8 216 4.091006
7
0.0 44.3 284 3.790985
8
3.7 45.1 221 3.808882
9
0.0 51.7 275 3.945458
10
3.0 50.0 269 3.912023
11
10.4 46.8 207 3.845883
12
0.0 58.0 249 4.060443
13
0.0 58.9 246 4.075841
14
0.0 40.0 277 3.688879
15
1.5 34.1 231 3.529297
16
0.0 39.9 219 3.686376
17
0.0 33.4 240 3.508556
18
3.5 48.1 266 3.873282
19
6.1 46.6 214 3.841601
20
0.0 53.1 251 3.972177
> tobin[tobin$age<40,]
durable age quant
15
1.5 34.1 231
16
0.0 39.9 219
17
0.0 33.4 240
>
> data1<-transform(tobin,log.quant=log(tobin$age))
> data1
durable age quant log.quant
1
0.0 57.7 236 4.055257
2
0.7 50.9 283 3.929863
3
0.0 48.5 207 3.881564
4
0.0 41.7 220 3.730501
5
0.0 47.7 238 3.864931
6
0.0 59.8 216 4.091006
7
0.0 44.3 284 3.790985
8
3.7 45.1 221 3.808882
9
0.0 51.7 275 3.945458
10
3.0 50.0 269 3.912023
11
10.4 46.8 207 3.845883
12
0.0 58.0 249 4.060443
13
0.0 58.9 246 4.075841
14
0.0 40.0 277 3.688879
15
1.5 34.1 231 3.529297
16
0.0 39.9 219 3.686376
17
0.0 33.4 240 3.508556
18
3.5 48.1 266 3.873282
19
6.1 46.6 214 3.841601
20
0.0 53.1 251 3.972177
> (data1<-transform(tobin,log.quant=log(tobin$age)))
durable age quant log.quant
1
0.0 57.7 236 4.055257
2
0.7 50.9 283 3.929863
3
0.0 48.5 207 3.881564
4
0.0 41.7 220 3.730501
5
0.0 47.7 238 3.864931
6
0.0 59.8 216 4.091006
7
0.0 44.3 284 3.790985
8
3.7 45.1 221 3.808882
9
0.0 51.7 275 3.945458
10
3.0 50.0 269 3.912023
11
10.4 46.8 207 3.845883
12
0.0 58.0 249 4.060443
13
0.0 58.9 246 4.075841
14
0.0 40.0 277 3.688879
15
1.5 34.1 231 3.529297
16
0.0 39.9 219 3.686376
17
0.0 33.4 240 3.508556
18
3.5 48.1 266 3.873282
19
6.1 46.6 214 3.841601
20
0.0 53.1 251 3.972177
> # add a () for the whole argument will show you the data you assigned to data1
>
>
>
> x<-y<-rep(0,10)
> x
[1] 0 0 0 0 0 0 0 0 0 0
> y
[1] 0 0 0 0 0 0 0 0 0 0
> x<-y<-rep(0,10); y<-rep(0,10)
>
> # semicolumn lets you write two lines of code at the same line
>
>
>
> data<-transform(tobin,+
+
+ log.quant=log(tobin$age)
Error: unexpected '=' in:
"
log.quant="
> data<-transform(tobin,
+ log.quant=log(tobin$age)
+
+
+ data
Error: unexpected symbol in:
"
data"
> data<-transform(tobin,
+ log.quant=log(tobin$age))
> data
durable age quant log.quant
1
0.0 57.7 236 4.055257
2
0.7 50.9 283 3.929863
3
0.0 48.5 207 3.881564
4
0.0 41.7 220 3.730501
5
0.0 47.7 238 3.864931
6
0.0 59.8 216 4.091006
7
0.0 44.3 284 3.790985
8
3.7 45.1 221 3.808882
9
0.0 51.7 275 3.945458
10
3.0 50.0 269 3.912023
11
10.4 46.8 207 3.845883
12
0.0 58.0 249 4.060443
13
0.0 58.9 246 4.075841
14
0.0 40.0 277 3.688879
15
1.5 34.1 231 3.529297
16
0.0 39.9 219 3.686376
17
0.0 33.4 240 3.508556
18
3.5 48.1 266 3.873282
19
6.1 46.6 214 3.841601
20
0.0 53.1 251 3.972177
>
>
>
>
>
> data1
durable age quant log.quant
1
0.0 57.7 236 4.055257
2
0.7 50.9 283 3.929863
3
0.0 48.5 207 3.881564
4
0.0 41.7 220 3.730501
5
0.0 47.7 238 3.864931
6
0.0 59.8 216 4.091006
7
0.0 44.3 284 3.790985
8
3.7 45.1 221 3.808882
9
0.0 51.7 275 3.945458
10
3.0 50.0 269 3.912023
11
10.4 46.8 207 3.845883
12
0.0 58.0 249 4.060443
13
0.0 58.9 246 4.075841
14
0.0 40.0 277 3.688879
15
1.5 34.1 231 3.529297
16
0.0 39.9 219 3.686376
17
0.0 33.4 240 3.508556
18
3.5 48.1 266 3.873282
19
6.1 46.6 214 3.841601
20
0.0 53.1 251 3.972177
> plot(data1)
> plot(data1[,3:4])
> plot(data1[,3],data1[,4])
>
>
>
> x<-runif(50,0,2)
> plot(x)
>
> y<-runif(50,0,2)
> plot(x,y)
>
>
> plot(x,y,type="l")
> plot(x,y,type="p")
> plot(x,y,type="m")
Error in plot.xy(xy, type, ...) : invalid plot type 'm'
> plot(x,y,type="n")
>
>
> # l=line, p=point, n=now
>
>
>
>
>
> plot(x,y,type="p",col="red")
>
> plot(x,y,type="p",col="red")
> lines(x,y,col="green")
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yet
> plot(x,y,type="p",col="red")
> lines(x,y,col="green")
> plot(x,y,main="Main Title",sub="subtitle",xlab="x-label",ylab="y-label")
>
>
> #notice that plot can only called once....
>
>
>
>
>
>
> text(0.6,0.6,"text at (0.6,0.6)")
> text(0.6,0.6,seq(1,50))
> text(0.8,0.8,seq(1,50))
> text(0.8,0.8,seq(1,5))
> text(0.4,0.4,seq(1,5))
> text(0.1,0.4,seq(1,3))
>
>
>
> abline(h=0.6,v=0.6)
> abline(h=0.6,v=0.6,col="blue")
> for(side in 1:4) mtext(-1:4,side=side,at=0.7,line=1:4)
> mtext(paste("side",1:4),side=1:4,line=-1,font=2)
>
> paste("side",1:4)
[1] "side 1" "side 2" "side 3" "side 4"
> paste("side",1:4, sep="*")
[1] "side*1" "side*2" "side*3" "side*4"
> paste("side",matrix(6,2,3), sep="*")
.49078702 0.32650147
[17] 0.37551686 -0.33339984 1.92139656 -1.09313111 0.81379273 0.86107231
.99257399 -0.18233715
[25] -1.63456505 0.55465888 0.90696307 -1.29818505 -1.09071733 -0.29949034
.16337660 -2.28855377
[33] 1.19115452 0.88330987 0.71214876 -0.84559574 1.13281535 0.04427004
.57274957 0.10955200
[41] -0.02050210 0.77821930 -0.37075093 -0.71096780 0.12048946 0.49053467
.32126920 -0.17167234
[49] 1.20343604 -1.18442786 -2.30671881 1.78166009 0.26733831 0.23065217
.72684946 0.62224890
[57] 0.05597812 0.12284053 1.28916847 0.85095395 -0.98620645 -0.01807000
.99057800 0.06375404
[65] 0.72583094 -1.64438273 1.17461543 -0.01997173 1.58899555 0.13835791
.09182709 0.11419373
[73] -0.30587088 1.38797393 0.64629151 0.03990076 0.93514254 0.03455621
.34391823 0.82315650
[81] -1.07732068 1.34134199 0.12798760 -0.26978970 -1.05396129 0.00636573
.33282233 0.29127687
[89] -0.25657841 -2.15031233 0.15663228 0.68545409 -1.49260002 0.55453350
.08990230 0.34390482
[97] -2.47867190 3.05339469 -0.57717748 1.21280389
> hist(x)
> hist(x,freq=F)
> curve(dnorm(x),add=T)
> dnorm(x)
[1] 0.200428079 0.340759743 0.060855810 0.259784797 0.386206691 0.343112357
391627323 0.392175693
[9] 0.011242368 0.225191269 0.395210698 0.021241090 0.340821194 0.130302601
353675844 0.378234790
[17] 0.371782977 0.377374861 0.062987379 0.219499277 0.286485362 0.275364035
243767567 0.392365313
[25] 0.104890326 0.342062514 0.264416561 0.171773121 0.220078566 0.381446084
393653385 0.029080783
[33] 0.196250557 0.270074700 0.309586897 0.279024800 0.210015522 0.398551541
338591954 0.396555468
[41] 0.398858444 0.294713619 0.372444718 0.309847159 0.396056901 0.353719638
166657464 0.393106690
[49] 0.193385884 0.197824855 0.027892028 0.081586222 0.384937837 0.388470228
306329610 0.328724461
[57] 0.398317717 0.395943628 0.173788504 0.277759445 0.245308147 0.398877153
244250505 0.398132338
[65] 0.306556313 0.103215545 0.200127775 0.398862725 0.112884290 0.395142038
397263838 0.396349597
[73] 0.380710121 0.152258681 0.323749604 0.398624835 0.257641983 0.398704157
376032987 0.284298128
[81] 0.223297916 0.162262854 0.395688123 0.384684494 0.228926169 0.398934197
164121838 0.382372643
[89] 0.386024371 0.039523491 0.394078414 0.315416217 0.130959659 0.342086300
397333323 0.376034722
[97] 0.018484075 0.003770498 0.337731035 0.191209581
>
>
> h<-hist(x,plot=F)
>
> names(h)
[1] "breaks" "counts" "density" "mids"
"xname"
"equidist"
> h$breaks
[1] -3 -2 -1 0 1 2 3 4
> h$counts
-0
0
0
1
-0
-0
-0
0
1
0
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
[1] 5 11 22 45 15 1 1
> h$density
[1] 0.05 0.11 0.22 0.45 0.15 0.01 0.01
> ylim<-range(0,h$density,dnorm(0))
> ylim
[1] 0.00 0.45
> hist(x,freq=F,ylim=ylim)
> curve(dnorm(x),add=T)
> x<-rnorm(10^5)
> hist(x)
> hist(x,100)
> hist(x,10)
> hist(x,100)
> hist(x,100,freq=f)
Error in hist.default(x, 100, freq = f) : object 'f' not found
> hist(x,100,freq=F)
> curve(dnorm(x),add=T,col="red")
>
>
>
>
>
>
> hist.with.normal <- function(x,
+ xlab=deparse(substitute(x)),...){
+ h <- hist(x, plot=F, ...)
+ s <- sd(x)
+ m <- mean(x)
+ ylim <- range(0,h$density,dnorm(0,sd=s))
+ hist(x, freq=F, ylim=ylim, xlab=xlab, ...)
+ curve(dnorm(x,m,s), add=T)
+ }
>
>
> hist.with.normal
function(x,
xlab=deparse(substitute(x)),...){
h <- hist(x, plot=F, ...)
s <- sd(x)
m <- mean(x)
ylim <- range(0,h$density,dnorm(0,sd=s))
hist(x, freq=F, ylim=ylim, xlab=xlab, ...)
curve(dnorm(x,m,s), add=T)
}
> hist.with.normal(x)
> hist.with.normal(rnorm(10^3))
> hist.with.normal(rnorm(10^3))
> hist.with.normal(rnorm(10^3))
> hist.with.normal(rnorm(10^3))
> hist.with.normal(rnorm(10^3))
> hist.with.normal(rnorm(10^3))
> hist.with.normal(rnorm(10^3))
> hist.with.normal(rnorm(10^3))
> hist.with.normal(rnorm(10^3))
>
>
>
>
>
>
> # loo[]
> # loop
>
>
>
> y<-12345
> x<-y/2
> while (abs(x*x-y) > 1e-10) x <- (x + y/x)/2
> x
[1] 111.1081
> y-x^2
[1] -5.456968e-12
> x<-y/2
> repeat {
+ x <- (x + y/x)/2
+ if (abs(x*x-y) < 1e-10) break}
> x
[1] 111.1081
>
> for(i in 1:15){cat("hell","\n")}
hell
hell
hell
hell
hell
hell
hell
hell
hell
hell
hell
hell
hell
hell
hell
> # cat means output sth on screen
>
>
>
>
> x<-seq(0,1,0.05)
> plot(x, x, ylab="y", type="l")
> > for ( j in 2:8 ) lines(x, x^j)
Error: unexpected '>' in ">"
> plot(x, x, ylab="y", type="l")
> for ( j in 2:8 ) lines(x, x^j)
>
>
>
>
> t.test
function (x, ...)
UseMethod("t.test")
<bytecode: 0x7ff88c05c868>
<environment: namespace:stats>
> ? t.test
>
>
>
>
> t.test(rnorm(100),mu=o)
Error in t.test.default(rnorm(100), mu = o) : object 'o' not found
> t.test(rnorm(100),mu=0)
One Sample t-test
data: rnorm(100)
t = 0.93217, df = 99, p-value = 0.3535
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-0.09919779 0.27498863
sample estimates:
mean of x
0.08789542
> t.test(rnorm(100),mu=0)
One Sample t-test
data: rnorm(100)
t = -0.50784, df = 99, p-value = 0.6127
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-0.2553848 0.1512982
sample estimates:
mean of x
-0.05204329
>