R to identify successful fundamental trading strategies for equity investments
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Workspace loaded from ~/.RData]
Loading required package: fOptions
Loading required package: timeDate
Loading required package: timeSeries
Loading required package: fBasics
> library(gdata)
gdata: Unable to locate valid perl
gdata: interpreter
gdata:
gdata: read.xls() will be unable to read
gdata: Excel XLS and XLSX files unless
gdata: the 'perl=' argument is used to
gdata: specify the location of a valid
gdata: perl intrpreter.
gdata:
gdata: (To avoid display of this message
gdata: in the future, please ensure perl
gdata: is installed and available on the
gdata: executable search path.)
gdata: Unable to load perl libaries
gdata: needed by read.xls()
gdata: to support 'XLX' (Excel 97-2004)
gdata: files.
gdata: Unable to load perl libaries
gdata: needed by read.xls()
gdata: to support 'XLSX' (Excel 2007+)
gdata: files.
gdata: Run the function
gdata: 'installXLSXsupport()'
gdata: to automatically download and
gdata: install the perl
gdata: libaries needed to support Excel
gdata: XLS and XLSX formats.
Attaching package: ‘gdata’
The following object is masked from ‘package:stats’:
nobs
The following object is masked from ‘package:utils’:
object.size
The following object is masked from ‘package:base’:
startsWith
Warning messages:
1: package ‘fOptions’ was built under R version 3.5.2
2: package ‘timeDate’ was built under R version 3.5.1
3: package ‘timeSeries’ was built under R version 3.5.1
4: package ‘fBasics’ was built under R version 3.5.2
5: package ‘gdata’ was built under R version 3.5.1
> Args(glm)
value
formula
family gaussian
data
weights
subset
na.action
start NULL
etastart
mustart
offset
control list(...)
model TRUE
method "glm.fit"
x FALSE
y TRUE
singular.ok TRUE
contrasts NULL
...
> Args(scan)
value
file ""
what double()
nmax -1
n -1
sep ""
quote if (identical(sep, "\\n")) "" else "'\\""
dec "."
skip 0
nlines 0
na.strings "NA"
flush FALSE
fill FALSE
strip.white FALSE
quiet FALSE
blank.lines.skip TRUE
multi.line TRUE
comment.char ""
allowEscapes FALSE
fileEncoding ""
encoding "unknown"
text
skipNul FALSE
> Args(legend, sort=TRUE)
value
adj c(0, 0.5)
angle 45
bg par("bg")
border "black"
box.col par("fg")
box.lty par("lty")
box.lwd par("lwd")
bty "o"
cex 1
col par("col")
density NULL
fill NULL
horiz FALSE
inset 0
legend
lty
lwd
merge do.lines && has.pch
ncol 1
pch
plot TRUE
pt.bg NA
pt.cex cex
pt.lwd lwd
seg.len 2
text.col par("col")
text.font NULL
text.width NULL
title NULL
title.adj 0.5
title.col text.col
trace FALSE
x
x.intersp 1
xjust 0
xpd
y NULL
y.intersp 1
yjust 1
> f1 <- factor(letters[1:10])
> f2 <- factor(letters[5:14])
> as.integer(f1)
[1] 1 2 3 4 5 6 7 8 9 10
> as.integer(f2)
[1] 1 2 3 4 5 6 7 8 9 10
> mapCha1 <- mapLevels(f1, codes=FALSE)
> mapCha2 <- mapLevels(f2, codes=FALSE)
> (mapCha <- c(mapCha1, mapCha2))
a b c d e f g h i j k
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k"
l m n
"l" "m" "n"
> mapLevels(f1) <- mapCha # the same as levels(f1) <- mapCha
> mapLevels(f2) <- mapCha # the same as levels(f2) <- mapCha
> f1 <- as.character(f1); f2 <- as.character(f2)
> mapLevels(f1) <- mapCha
> mapLevels(f2) <- mapCha
> object.size(letters)
[1] 1712 bytes
> object.size(ls)
[1] 89096 bytes
> allObj <- sapply(ls("package:base"),
+ function(x)
+ object.size(get(x, envir = baseenv()))
+ )
> ( bigObj <- as.object_sizes(rev(sort(allObj))[1:10] ) )
[1] 2244184 bytes 1558560 bytes 1156952 bytes
[4] 968192 bytes 818944 bytes 706272 bytes
[7] 682432 bytes 567672 bytes 555432 bytes
[10] 535936 bytes
> print(bigObj, humanReadable=TRUE)
[1] 2.1 MiB 1.5 MiB 1.1 MiB 945.5 KiB
[5] 799.8 KiB 689.7 KiB 666.4 KiB 554.4 KiB
[9] 542.4 KiB 523.4 KiB
> as.object_sizes(14567567)
[1] 14567567 bytes
> options(humanReadable=TRUE)
>
> (
+ z <- object.size(letters,
+ c(letters, letters),
+ rep(letters, 100),
+ rep(letters, 10000)
+ )
+ )
[1] 1.7 KiB 1.9 KiB 21.8 KiB 2.0 MiB
>
> is.object_sizes(z)
[1] TRUE
> as.object_sizes(14567567)
[1] 13.9 MiB
> n1 <- 6
> n2 <- 12
> n3 <- 4
> num <- c(5:n1, 10:13)
> (tmp1 <- data.frame(y1=rnorm(n=n1),
+ f1=factor(rep(c("A", "B"), n1/2)),
+ ch=letters[num],
+ fa=factor(letters[num]),
+ nu=(num) + 0.5,
+ id=factor(num), stringsAsFactors=FALSE))
y1 f1 ch fa nu id
1 1.34501889 A e e 5.5 5
2 -0.83806595 B f f 6.5 6
3 0.11467005 A j j 10.5 10
4 -1.92993351 B k k 11.5 11
5 0.88410486 A l l 12.5 12
6 0.09300388 B m m 13.5 13
> num <- 4:9
>
> (tmp2 <- data.frame(y2=rnorm(n=n2),
+ f2=factor(rep(c("C", "D"), n2/2)),
+ ch=letters[rep(num, times=2)],
+ fa=factor(letters[rep(c(num), times=2)]),
+ nu=c((num) + 0.5, (num) + 0.25),
+ id=factor(rep(num, times=2)), stringsAsFactors=FALSE))
y2 f2 ch fa nu id
1 0.5123710 C d d 4.50 4
2 -2.1244430 D e e 5.50 5
3 1.1766990 C f f 6.50 6
4 0.6765940 D g g 7.50 7
5 1.6737569 C h h 8.50 8
6 -1.3849047 D i i 9.50 9
7 1.3999856 C d d 4.25 4
8 -0.7676680 D e e 5.25 5
9 -0.1873588 C f f 6.25 6
10 -0.4829591 D g g 7.25 7
11 0.4005822 C h h 8.25 8
12 -0.9674152 D i i 9.25 9
> num <- 1:4
> (tmp3 <- data.frame(y3=rnorm(n=n3),
+ f3=factor(rep(c("E", "F"), n3/2)),
+ ch=letters[num],
+ fa=factor(letters[num]),
+ nu=(num) + 0.5,
+ id=factor(num), stringsAsFactors=FALSE))
y3 f3 ch fa nu id
1 -0.08051201 E a a 1.5 1
2 -0.17400201 F b b 2.5 2
3 -1.85368938 E c c 3.5 3
4 0.42231442 F d d 4.5 4
> (tmp12 <- bindData(x=tmp1, y=tmp2, common=c("id", "nu", "ch", "fa")))
id nu ch fa y1 f1 y2
1 5 5.50 e e 1.34501889 A NA
2 6 6.50 f f -0.83806595 B NA
3 10 10.50 j j 0.11467005 A NA
4 11 11.50 k k -1.92993351 B NA
5 12 12.50 l l 0.88410486 A NA
6 13 13.50 m m 0.09300388 B NA
7 4 4.50 d d NA <NA> 0.5123710
8 5 5.50 e e NA <NA> -2.1244430
9 6 6.50 f f NA <NA> 1.1766990
10 7 7.50 g g NA <NA> 0.6765940
11 8 8.50 h h NA <NA> 1.6737569
12 9 9.50 i i NA <NA> -1.3849047
13 4 4.25 d d NA <NA> 1.3999856
14 5 5.25 e e NA <NA> -0.7676680
15 6 6.25 f f NA <NA> -0.1873588
16 7 7.25 g g NA <NA> -0.4829591
17 8 8.25 h h NA <NA> 0.4005822
18 9 9.25 i i NA <NA> -0.9674152
f2
1 <NA>
2 <NA>
3 <NA>
4 <NA>
5 <NA>
6 <NA>
7 C
8 D
9 C
10 D
11 C
12 D
13 C
14 D
15 C
16 D
17 C
18 D
> (tmp123 <- bindData(x=tmp12, y=tmp3, common=c("id", "nu", "ch", "fa")))
id nu ch fa y1 f1 y2
1 5 5.50 e e 1.34501889 A NA
2 6 6.50 f f -0.83806595 B NA
3 10 10.50 j j 0.11467005 A NA
4 11 11.50 k k -1.92993351 B NA
5 12 12.50 l l 0.88410486 A NA
6 13 13.50 m m 0.09300388 B NA
7 4 4.50 d d NA <NA> 0.5123710
8 5 5.50 e e NA <NA> -2.1244430
9 6 6.50 f f NA <NA> 1.1766990
10 7 7.50 g g NA <NA> 0.6765940
11 8 8.50 h h NA <NA> 1.6737569
12 9 9.50 i i NA <NA> -1.3849047
13 4 4.25 d d NA <NA> 1.3999856
14 5 5.25 e e NA <NA> -0.7676680
15 6 6.25 f f NA <NA> -0.1873588
16 7 7.25 g g NA <NA> -0.4829591
17 8 8.25 h h NA <NA> 0.4005822
18 9 9.25 i i NA <NA> -0.9674152
19 1 1.50 a a NA <NA> NA
20 2 2.50 b b NA <NA> NA
21 3 3.50 c c NA <NA> NA
22 4 4.50 d d NA <NA> NA
f2 y3 f3
1 <NA> NA <NA>
2 <NA> NA <NA>
3 <NA> NA <NA>
4 <NA> NA <NA>
5 <NA> NA <NA>
6 <NA> NA <NA>
7 C NA <NA>
8 D NA <NA>
9 C NA <NA>
10 D NA <NA>
11 C NA <NA>
12 D NA <NA>
13 C NA <NA>
14 D NA <NA>
15 C NA <NA>
16 D NA <NA>
17 C NA <NA>
18 D NA <NA>
19 <NA> -0.08051201 E
20 <NA> -0.17400201 F
21 <NA> -1.85368938 E
22 <NA> 0.42231442 F
> tmp123[order(tmp123$ch), ]
id nu ch fa y1 f1 y2
19 1 1.50 a a NA <NA> NA
20 2 2.50 b b NA <NA> NA
21 3 3.50 c c NA <NA> NA
7 4 4.50 d d NA <NA> 0.5123710
13 4 4.25 d d NA <NA> 1.3999856
22 4 4.50 d d NA <NA> NA
1 5 5.50 e e 1.34501889 A NA
8 5 5.50 e e NA <NA> -2.1244430
14 5 5.25 e e NA <NA> -0.7676680
2 6 6.50 f f -0.83806595 B NA
9 6 6.50 f f NA <NA> 1.1766990
15 6 6.25 f f NA <NA> -0.1873588
10 7 7.50 g g NA <NA> 0.6765940
16 7 7.25 g g NA <NA> -0.4829591
11 8 8.50 h h NA <NA> 1.6737569
17 8 8.25 h h NA <NA> 0.4005822
12 9 9.50 i i NA <NA> -1.3849047
18 9 9.25 i i NA <NA> -0.9674152
3 10 10.50 j j 0.11467005 A NA
4 11 11.50 k k -1.92993351 B NA
5 12 12.50 l l 0.88410486 A NA
6 13 13.50 m m 0.09300388 B NA
f2 y3 f3
19 <NA> -0.08051201 E
20 <NA> -0.17400201 F
21 <NA> -1.85368938 E
7 C NA <NA>
13 C NA <NA>
22 <NA> 0.42231442 F
1 <NA> NA <NA>
8 D NA <NA>
14 D NA <NA>
2 <NA> NA <NA>
9 C NA <NA>
15 C NA <NA>
10 D NA <NA>
16 D NA <NA>
11 C NA <NA>
17 C NA <NA>
12 D NA <NA>
18 D NA <NA>
3 <NA> NA <NA>
4 <NA> NA <NA>
5 <NA> NA <NA>
6 <NA> NA <NA>
> df1 <- data.frame(a=1:3, b=c("A", "B", "C"))
> df2 <- data.frame(c=as.character(1:5), a=5:1)
> ma1 <- matrix(as.character(1:4), nrow=2, ncol=2)
> ma2 <- matrix(1:6, nrow=3, ncol=2)
> cbindX(df1, df2)
a b c a
1 1 A 1 5
2 2 B 2 4
3 3 C 3 3
4 NA <NA> 4 2
5 NA <NA> 5 1
> cbindX(ma1, ma2)
[,1] [,2] [,3] [,4]
[1,] "1" "3" "1" "4"
[2,] "2" "4" "2" "5"
[3,] NA NA "3" "6"
> cbindX(df1, ma1)
a b 1 2
1 1 A 1 3
2 2 B 2 4
3 3 C <NA> <NA>
> cbindX(df1, df2, ma1, ma2)
a b c a 1 2 1 2
1 1 A 1 5 1 3 1 4
2 2 B 2 4 2 4 2 5
3 3 C 3 3 <NA> <NA> 3 6
4 NA <NA> 4 2 <NA> <NA> NA NA
5 NA <NA> 5 1 <NA> <NA> NA NA
> cbindX(ma1, ma2, df1, df2)
1 2 3 4 a b c a
1 1 3 1 4 1 A 1 5
2 2 4 2 5 2 B 2 4
3 <NA> <NA> 3 6 3 C 3 3
4 <NA> <NA> <NA> <NA> NA <NA> 4 2
5 <NA> <NA> <NA> <NA> NA <NA> 5 1
> cat(centerText("One Line Test"), "\n\n")
One Line Test
> mText <-c("This", "is an example",
+ " of a multiline text ",
+ "with ",
+ " leading",
+ " and trailing ",
+ "spaces.")
> cat("\n", centerText(mText), "\n", sep="\n")
This
is an example
of a multiline text
with
leading
and trailing
spaces.
> a <- matrix(rnorm(12),ncol=4,nrow=3)
> b <- 1:4
> combine(a,b)
X1 X2 X3
X 1.867320 0.9797075 1.15804502
X.1 1.419801 0.5619077 0.05526391
X.2 -1.616789 0.3666752 0.94908855
b 1.000000 2.0000000 3.00000000
X4 source
X 0.36399708 a
X.1 -0.67280322 a
X.2 -0.09014888 a
b 4.00000000 b
> combine(x=a,b)
X1 X2 X3
X 1.867320 0.9797075 1.15804502
X.1 1.419801 0.5619077 0.05526391
X.2 -1.616789 0.3666752 0.94908855
b 1.000000 2.0000000 3.00000000
X4 source
X 0.36399708 x
X.1 -0.67280322 x
X.2 -0.09014888 x
b 4.00000000
> combine(x=a,y=b)
X1 X2 X3
X 1.867320 0.9797075 1.15804502
X.1 1.419801 0.5619077 0.05526391
X.2 -1.616789 0.3666752 0.94908855
y 1.000000 2.0000000 3.00000000
X4 source
X 0.36399708 x
X.1 -0.67280322 x
X.2 -0.09014888 x
y 4.00000000 y
> combine(a,b,names=c("one","two"))
X1 X2 X3
X 1.867320 0.9797075 1.15804502
X.1 1.419801 0.5619077 0.05526391
X.2 -1.616789 0.3666752 0.94908855
b 1.000000 2.0000000 3.00000000
X4 source
X 0.36399708 one
X.1 -0.67280322 one
X.2 -0.09014888 one
b 4.00000000 two
0 Comments