Professional Documents
Culture Documents
splus - βασικά πράγματα
splus - βασικά πράγματα
2003
1
2
3
4
5
..................................................................................................................... 1
.......................................................................................................... 2
............................................................................. 11
................................................................................................................. 13
e if, while, for................................................................................................. 14
5.1
if............................................................................................................ 14
5.2
for ......................................................................................................... 15
5.3
while ..................................................................................................... 17
6
.............................................................................................................. 18
7
................................................................................................................ 22
SPLUS
1
SPLUS
SPLUS
.
SPLUS.
!" SPLUS R
.
R www.r-project.org.
.
.
R.
R ,
, ( ).
.
!" SPLUS
(.. Fortran, C++).
.
. SPLUS
.
SPLUS.
!
!" SPLUS ( Commands)
. >
, +
,
[1] .
<- (2 ) ,
. x<-5
x 5.
SPLUS
'_' (underscore),
x_5.
.
!" SPLUS
,
.
!"
.
!" SPLUS help . H SPLUS
.
!" SPLUS .
(
)
{}.
!" , SPLUS R
script.
.
.
!" # SPLUS
.
#
.
SPLUS.
2
SPLUS
, .
SPLUS .
:
> 5+5
[1] 10
>, [1]
. 2 .
# SPLUS.
SPLUS
.
,
> 3+sqrt(45)*exp(0.4)+8*cos(0.5)
[1] 20.02812
(sqrt) ,
e (exp) (cos).
, .
> x <- 3+sqrt(45)*exp(0.4)+8*cos(0.5)
> x
[1] 20.02812
x
.
,
SPLUS.
(number) ,
(vector),
(matrix) ,
(data.frame)
(list).
SPLUS (objects). o
SPLUS, .
/
. SPLUS
.
.
x y
. , SPLUS
.
> x<-4
> y<-5
> x+y
[1] 9
> x/y
[1] 0.8
> x==y
[1] F
> x%%y
[1] 4
SPLUS
> x%/%y
[1] 0
> x^2+8
[1] 24
SPLUS
+
*
/
^
%/%
%%
,
1. SPLUS
,
, .
,
'' .
.
== ,
T (true) F
(false) . (
0 1) .
>
<
>=
<=
==
!=
2. SPLUS
2 &
|. & T |
print ,
>print(x)
[1] 4
SPLUS
.
c( ),
.
> x<-c(1,2,3,4,5)
> y<-c(2,5,3,-4,9)
> z<-c(x,y)
> x
[1] 1 2 3 4 5
> y
[1] 2 5 3 -4 9
> z
[1] 1 2 3 4 5
3 -4
x 5 1, 2, 3, 4 5
. y, z
x y. z 10 ,
5 x y.
, SPLUS
- . seq, rep
a:b .
> x<-1:5
> x
[1] 1 2 3 4 5
w<-rep(0.5,8)
> w
[1] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
> w<-rep(NA,4)
> w
[1] NA NA NA NA
> w<-seq(1,15,length=5)
> w
[1] 1.0 4.5 8.0 11.5 15.0
> 3.2:9
[1] 3.2 4.2 5.2 6.2 7.2 8.2
3 :
:b
seq(a,b,length=d)
1 b.
d.
a
b
rep(a,b)
H b a.
NA missing data (not available)
3. SPLUS
SPLUS
..
> rep(1:4,3)
[1] 1 2 3 4 1 2 3 4 1 2 3 4
3 1 2 3 4
.
help
.
.
. sqrt
NA.
( )
sum . sum(y)
.
.
. x[1]
x x[1:4]
4 x.
T F,
.
> x
[1] 1 2 3 4 5
> y
[1] 2 5 3 -4 9
> x+y
[1] 3 7 6 0 14
> x-y
[1] -1 -3 0 8 -4
> x*y
[1]
2 10
9 -16 45
> x+sqrt(y)
[1] 2.414214 4.236068 4.732051
> x+sum(y)
[1] 16 17 18 19 20
> x[1]
[1] 1
> x[2:4]+y[1:3]
NA 8.000000
SPLUS
[1] 4 8 7
> x[1]<-100
> x
[1] 100
2
> x[x<50]
[1] 2 3 4 5
:
> x<-c(2,3,2,3,2)
> y<-c(1,2)
> x*y
[1] 2 6 2 6 2
Warning messages:
Length of longer
shorter object in: x
> z<-x+y
Warning messages:
Length of longer
shorter object in: x
> z
[1] 3 5 3 5 3
object
* y
is
not
multiple
of
the
length
of
the
object
+ y
is
not
multiple
of
the
length
of
the
.
.
,
..
cbind
>
>
>
>
x<-c(1,2,3,4,5)
y<-c(1,2,1,2,-1)
z<-cbind(x,y)
z
x y
[1,] 1 1
[2,] 2 2
[3,] 3 1
[4,] 4 2
[5,] 5 1
z 5 2 .
,
> cbind(z,y)
x y y
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 1 1
SPLUS
[4,] 4 2 2
[5,] 5 -1 -1
SPLUS.
matrix .
> x<-1:6
> z<-matrix(x,2,3)
> z
[,1] [,2] [,3]
[1,]
1
3
5
[2,]
2
4
6
> z<-matrix(x,2,3,byrow=F)
> z
[,1] [,2] [,3]
[1,]
1
3
5
[2,]
2
4
6
> z<-matrix(x,2,3,byrow=T)
> z
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
z<-matrix(NA,2,3) 2 3
NA. . ,
x 6 .
byrow=F (
byrow=F )
x .
byrow.
.
( +, -, * , / , ^ ) , , ,
.
.
.
.
Q :
T(Q)
solve(Q)
%*%
Q
Q ( )
( ,
)
4.
> Q<-matrix(c(1,0.2,0,0.2,1,0,0,0,1),3,3)
SPLUS
> Q
[,1] [,2] [,3]
[1,] 1.0 0.2
0
[2,] 0.2 1.0
0
[3,] 0.0 0.0
1
> t(Q)
[,1] [,2] [,3]
[1,] 1.0 0.2
0
[2,] 0.2 1.0
0
[3,] 0.0 0.0
1
> solve(Q)
[,1]
[,2] [,3]
[1,] 1.0416667 -0.2083333
0
[2,] -0.2083333 1.0416667
0
[3,] 0.0000000 0.0000000
1
> Q*solve(Q)
[,1]
[,2] [,3]
[1,] 1.04166667 -0.04166667
0
[2,] -0.04166667 1.04166667
0
[3,] 0.00000000 0.00000000
1
> Q%*%solve(Q)
[,1]
[,2] [,3]
[1,] 1.000000e+000 5.551115e-017
0
[2,] -5.551115e-017 1.000000e+000
0
[3,] 0.000000e+000 0.000000e+000
1
. , ,
.
.
,
.
x<-matrix(rnorm(100,0,1),50,2)
> mean(x[,1])
[1] -0.221015
> x[1,]
[1] -0.005494457 1.714466251
: .
,
Y = a + X
10
X
Y
4
8.5
6
11
7
12
12
20
4
9
SPLUS:
3
5.5
2
11
4
5
1
2.3
8
12
SPLUS
SPLUS
= ( X ' X ) 1 X 'Y
>
>
>
>
>
>
>
x<-c(4,6,7,12,4,3,2,4,1,8)
y<-c(8.5,11,12,20,9,5.5,11,5,2.3,12)
n<-length(x)
Y<-matrix(y,n,1)
#capital Y
X<-cbind(1,x)
#capital X
betahat<-solve(t(X)%*%X)%*%t(X)%*%Y
betahat
[,1]
2.876396
x 1.324236
: length,
, .
.
cbind SPLUS
.
SPLUS
> lm(y~x)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept)
x
2.876396 1.324236
Degrees of freedom: 10 total; 8 residual
Residual standard error: 2.500106
.
formula: y ~ x. SPLUS (formulas)
: (response)
( ~ )
.
.
SPLUS
.
formula .
,
,
, .
> reslm<-lm(y~x)
10
SPLUS
> reslm
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept)
x
2.876396 1.324236
Degrees of freedom: 10 total; 8 residual
Residual standard error: 2.500106
reslm
, , .
. reslm, SPLUS
, .
> reslm$fitted
1
2
3
4
5
6
7
8
9
10
8.17334 10.82181 12.14605 18.76723 8.17334 6.849104 5.524868 8.17334 4.200632 13.47028
> reslm$residuals
1
2
3
4
5
6
7
8
9
10
0.3266596 0.1781876 -0.1460485 1.232771 0.8266596 -1.349104 5.475132 -3.17334 -1.900632 -1.470285
> reslm$coeff
(Intercept)
x
2.876396 1.324236
3
.
,
ASCII.
example1.txt
drive C:
4
6
7
12
4
3
2
4
1
8
8.5
11.0
12.0
20.0
9.0
5.5
11.0
5.0
2.3
12.0
11
SPLUS
scan
exa1<-scan("c:\\example1.txt")
> exa1
[1] 4.0 8.5 6.0 11.0 7.0 12.0 12.0 20.0
11.0
[15] 4.0 5.0 1.0 2.3 8.0 12.0
>
4.0
9.0
3.0
5.5
2.0
SPLUS \\ .
.
. matrix.
.
matrix scan
> data<-matrix(scan("c:\\example1.txt"),10,2,byrow=T)
> data
[,1] [,2]
[1,]
4 8.5
[2,]
6 11.0
[3,]
7 12.0
[4,]
12 20.0
[5,]
4 9.0
[6,]
3 5.5
[7,]
2 11.0
[8,]
4 5.0
[9,]
1 2.3
[10,]
8 12.0
>
> exa1<-scan("c:\\example1.txt")
> data<-matrix(exa1,10,2,byrow=T)
.
read.table :
> data<-read.table("c:\\example1.txt")
> data
V1
V2
1 4 8.5
2 6 11.0
3 7 12.0
4 12 20.0
5 4 9.0
6 3 5.5
7 2 11.0
8 4 5.0
9 1 2.3
10 8 12.0
12
SPLUS
, .
data ,
. data[ ,1]
summary.
> summary(data[,1])
Min. 1st Qu. Median Mean 3rd Qu. Max.
1
3.25
4 5.1
6.75
12
> x2<-data[5,]
> x2
V1 V2
5 4 9
4
SPLUS
. .
(
) ,
. .
r
d
P
q
rnorm(n,0,1)
dnorm(x,0,1)
pnorm(x,0,1)
qnorm(p,0,1)
5.
, ( ),
, Poisson, , Student-t, , ,
, 2 , F .
help.
.
n , x
p
.
> rnorm(10,0,5)
[1] -3.27466840
7.81655283 -1.48391403
13
SPLUS
[8]
1.32666642 -1.27998524
0.01179473
> rpois(10,1)
[1] 0 1 0 2 0 0 1 0 0 1 4
> dpois(0:10,5)
[1] 0.006737947 0.033689735 0.084224337 0.140373896 0.175467370 0.175467370 0.146222808
[8] 0.104444863 0.065278039 0.036265577 0.018132789
> qnorm(c(0.025,0.05,0.50,0.90,0.95,0.975),0,1)
[1] -1.959964 -1.644854 0.000000 1.281552 1.644854
> pnorm(c(1.645,1.96),0,1)
[1] 0.9500151 0.9750021
>
1.959964
10
0 5. 10
Poisson , 1.
P( X = k ) k=0,1, ..., 10 Poisson 5.
c ( )
2.5%, 5%, 50%, 90%, 95% 97.5%.
( 0
1) 1.645 1.96.
5 E IF , WHILE , FOR
SPLUS
.
for, if while.
5.1 if
if
.
If
(true)
.
.
> x<-5
> y<-4
> if (x>=5) y<-0
[1] 0
> print(c(x,y))
[1] 5 0
> if (x<5) y<-10
NULL
> print(c(x,y))
[1] 5 0
if SPLUS
. NULL
14
SPLUS
(false) . if
> x<-5
> y<-4
> if (x<=5) {y<-10
+ x<-x^2
+ }
[1] 25
> print(c(x,y))
[1] 25 10
>
If () '1'
else
'2'
.
'1' '2' '1'
'2' .
else if,
'1'
SPLUS if .
:
> x<-4
> y<-5
> if (x<5) { y<-0
+ x<-x^2
+ } else {
+ x<-0
+ y<-10
+ }
[1] 16
> print(c(x,y))
[1] 16 0
,
. (x^2+5 + sqrt(x+4)<0)
x .
5.2 for
for while
.
for
while
.
15
SPLUS
for
for (name in values)
name values.
for
1:100 .
> t<-c(1,2,4,5)
> x<-c(10,15,25,34,55)
> for (i in t) x[i]<- - x[i]
> x
[1] -10 -15 25 -34 -55
> y<-rep(2,5)
> z<-1:5*0
> z
[1] 0 0 0 0 0
> for (i in 1:5) {
+ x[i]<-x[i]^2
+ y[i]<-y[i]+z[i]+i
+ }
> print(c(x,y,z))
[1] 100 225 625 1156 3025
[13]
0
0
0
t
i . 3
.
. z .
for .
.
x
x^32.
> x<-c(10,15,25,34,55)
> for (i in 1:5) x<-x^2
> x
[1] 1.000000e+032 4.314399e+037 5.421011e+044 1.017010e+049
[5] 4.915934e+055
for
.
!"
for
!" SPLUS
. .
16
SPLUS
,
> x<-rnorm(10000,0,1)
> x<-x+1
> for (i in 1:10000) x[i]<-x[i]+1
!" for.
.
>
>
+
+
+
>
x<-matrix(1:16,4,4)
for (i in 1:4) {
for (j in 1:4) { x[i,j]<--x[i,j]
}
}
x
[,1] [,2] [,3] [,4]
[1,]
-1
-5
-9 -13
[2,]
-2
-6 -10 -14
[3,]
-3
-7 -11 -15
[4,]
-4
-8 -12 -16
5.3 while
while
.
while
()
:
!"
.
!" .
70%
. , SPLUS
while
.
5 0.001
0.70.
.
.
0.001. E ,
,
17
SPLUS
> t <- -5
>
testvar <- 0
>
while(testvar != 10) {
+
t <- t + 0.001
+
if(pnorm(t, 0, 1) >= 0.70)
+
testvar <- 10
+
}
>
t
[1] 0.525
> qnorm(0.7,0,1)
[1] 0.5244005
for
.
> t <- -5
> while(pnorm(t, 0, 1) <= 0.70) {
+ t <- t + 0.001
+ }
> t
[1] 0.525
for while
while.
6
SPLUS
( , )
( , ) (
). SPLUS
.
.
.
. SPLUS var(
) .
>
+
+
>
stdev<-function(x) {
sqrt(var(x))
}
y<-rnorm(100,0,1)
18
SPLUS
> stdev(y)
[1] 1.012112
.
stdev , standdev
standard.deviation. '<' function.
,
.
,
.
x
. y.
.
,
(
) ,
.
list
,
.
, ,
, .
function .
testfunc<-funtion(x,n) {
...
...
}
x n.
testfunc(y,nnew)
y nnew .
testfunc(nnew,y) testfunc(y) .
SPLUS (copy-paste).
19
SPLUS
, SPLUS, Script.
File > New > Script. ,
.
.
.
.
2 .
trimmedmean<-function(x) {
mesitimi<-mean(x)
n<-length(x)
std<-stdev(x) #or std<-sqrt(var(x))
sum1<-0
metr<-0
for (i in 1:n) {
if (abs((x[i]-mesitimi)/std) <=2) {
sum1<-sum1+x[i]
metr<-metr+1
}
} # end of for
sum1/metr
}
.
. if,
for. .
> x<-rnorm(1000,0,1)
> trimmedmean(x)
[1] 0.001803929
> y<-rgamma(150,1,1)
> trimmedmean(y)
[1] 0.9367568
.
.
.
. '
, metr
,
.
trimmedmean<-function(x) {
mesitimi<-mean(x)
20
SPLUS
n<-length(x)
std<-stdev(x) #or std<-sqrt(var(x))
sum1<-0
metr<-0
for (i in 1:n) {
if (abs((x[i]-mesitimi)/std) <=2) {
sum1<-sum1+x[i]
metr<-metr+1
}
} # end of for
list(result=sum1/metr,numberofdata=metr)
}
:
> trimmedmean(x)
$result:
[1] 0.001803929
$numberofdata:
[1] 948
> trimmedmean(y)
$result:
[1] 0.9367568
$numberofdata:
[1] 143
SPLUS
list.
.
$
.
> xres<-trimmedmean(x)
> xres$numberofdat
[1] 948
> xres$result-mean(x)
[1] 0.01405705
> mean(x)
[1] -0.01225312
>
xres$result
.
, 2
.
21
SPLUS
( )
. :
trimmedmean<-function(x,nst) {
mesitimi<-mean(x)
n<-length(x)
std<-stdev(x) #or std<-sqrt(var(x))
sum1<-0
metr<-0
for (i in 1:n) {
if (abs((x[i]-mesitimi)/std) <=nst) {
sum1<-sum1+x[i]
metr<-metr+1
}
} # end of for
list(result=sum1/metr,numberofdata=metr)
}
,
> trimmedmean(x,1.5)
$result:
[1] 0.002804024
$numberofdata:
[1] 861
.
. :
!" ,
!" , . ,
trimmedmean
.
!"
7
SPLUS
.
.
plot.
1.
> x<-rnorm(100,0,5)
22
SPLUS
0.0
-1.0
-0.5
epsilon
0.5
1.0
> epsilon<-rnorm(100,0,0.5)
> plot(x,epsilon)
-10
-5
10
1.
hist. Word copy-paste.
10
10
15
20
15
25
20
30
> hist(x)
> hist(epsilon)
-10
-5
-1.5
10
-1.0
-0.5
0.0
0.5
1.0
1.5
epsilon
2.
, , , .
help .
.
.
3,
,
23
example
-20
-10
10
20
SPLUS
-10
-5
10
3.
>
>
>
>
y<-4+2*x+epsilon
plot(x,y)
abline(4,2)
text(-10,20,"example")
3 .
y. x y.
y x. 2 4,
y=4 +2x. epsilon
. (,
).
(-10,20).
SPLUS
. par
SPLUS.
2
2 , 4
4.
> par(mfrow=c(2,2))
> hist(x,nclasses=12)
24
SPLUS
hist(y,nclasses=12)
hist(epsilon,nclasses=12)
plot(x,y)
abline(4,2)
10
10
15
15
20
>
>
>
>
-10
-5
10
-20
-10
10
20
30
-20
-10
10
10
15 20 25
20
30
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
-10
-5
epsilon
10
4.
5
.
0 0.8
1 1.5.
t 15
.
x<-seq(-5,5,length=100)
y<-dnorm(x,0,0.8)
plot(x,y,type="l")
y<-dnorm(x,0,1)
lines(x,y)
y<-dnorm(x,0,1.5)
lines(x,y)
y<-dt(x,15)
lines(x,y)
text(0.5,0.5,"N(0,0.8)")
text(0,0.43,"N(0,1)")
text(0,0.36,"t(15)")
text(0,0.29,"N(0,1.5)")
25
0.5
SPLUS
N(0,0.8)
0.4
N(0,1)
0.3
t(15)
0.0
0.1
0.2
N(0,1.5)
-4
-2
5.
26