(Just Luck!)
Here is how we define a matrix. Give all the entries and the dimensions. Pay attention to
how R assigns the numbers to cell entries.
> b=matrix(c(1,2,3,4,5,6),3,2)
>b
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> b[2,1]
[1] 2
> b[3,2]
[1] 6
> t(b)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
> 2*b
[,1] [,2]
[1,] 2 8
[2,] 4 10
[3,] 6 12
> sum(b)
[1] 21
You can slice the rows or columns of matrix b.
> b[,1]
[1] 1 2 3
> b[3,]
[1] 3 6
You can generate regular sequences with R.
> a=1:10
>a
[1] 1 2 3 4 5 6 7 8 9 10
> b<-seq(1,10,by=1)
>b
[1] 1 2 3 4 5 6 7 8 9 10
> seq(-2,2,by=0.2)->c
>c
[1] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8
[16] 1.0 1.2 1.4 1.6 1.8 2.0
5. BASIC PLOTS
> x=c(1,2,3,4,5)
> y=c(2,3,3.3,4.4,6)
> plot(x,y)
> x=seq(-6,6,by=0.1)
> y=x^2
> plot(x,y)
x=seq(-2*pi,2*pi,by=0.05)
> y=sin(x)
> plot(x,y,type="l")
6. DESCRIPTIVE STATISTICS
> x=c(5,4,2,1,3)
> mean(x)
[1] 3
> min(x)
[1] 1
> max(x)
[1] 5
> sort(x)
[1] 1 2 3 4 5
> median(x)
[1] 3
> sd(x)
[1] 1.581139
> var(x)
[1] 2.5
Standardize the data points
> sx=(x-mean(x))/sd(x)
> sx
[1] 1.2649111 0.6324555 -0.6324555 -1.2649111 0.0000000
Simple Linear Regression
> x=c(1,2,3,4,5)
> y=c(2,3,3.3,4.4,6)
> lm(y~x)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept)
x
0.92
0.94
> plot(x,y)
> abline(lm(y~x))
> hist(x)
> mean(x)
[1] 1.748758
> sd(x)
[1] 3.938521
>boxplot(x)
Shapiro-Wilk test for testing normality (Null Hyppothesis: The data comes from a normal
distribution).
> shapiro.test(x)
Shapiro-Wilk normality test
data: x
W = 0.9901, p-value = 0.6733
Here, p-value is very large. No evidence for rejecting the null hypothesis that this data
comes from a normal distribution.
Now let us generate numbers from Beta distribution.
y=rbeta(100,1,4)
> hist(y)
> boxplot(y)
>qqnorm(y)
> shapiro.test(y)
Shapiro-Wilk normality test
data: y
W = 0.8616, p-value = 3.249e-08
Here p-value is very small, the null hypothesis of normality is rejected.
7. MATRIX ALGEBRA
> a=matrix(c(1,2,3,4),2,2)
>a
[,1] [,2]
[1,] 1 3
[2,] 2 4
> b=matrix(c(10,20,30,40),2,2)
>b
[,1] [,2]
[1,] 10 30
[2,] 20 40
> a+b
[,1] [,2]
[1,] 11 33
[2,] 22 44
> a-b
[,1] [,2]
[1,] -9 -27
[2,] -18 -36
Transpose
> t(a)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[,2]
[,3]
0.000000 e+00
0
1.000000 e+00
0
8.881784 e-16
1
Good enough!
> eigen(a)
$values
[1] -3 2 -2
$vectors
[,1] [,2]
[,3]
[1,] -0.4082483 0.9370426 -0.5773503
[2,] -0.8164966 0.3123475 0.5773503
[3,] -0.4082483 0.1561738 0.5773503
Now let us check if a*v1=e1*v1. First we must extract the first column of the above
matrix.
> L=eigen(a)
> L$vectors
[,1]
[,2]
[,3]
[1,] -0.4082483 0.9370426 -0.5773503
[2,] -0.8164966 0.3123475 0.5773503
[3,] -0.4082483 0.1561738 0.5773503
> v1=L$vectors[,1]
> v1
[1] -0.4082483 -0.8164966 -0.4082483
> a%*%v1
[,1]
[1,] 1.224745
[2,] 2.449490
[3,] 1.224745
> -3*v1
[1] 1.224745 2.449490 1.224745
Singular Value Decomposition
>a
[,1] [,2] [,3]
[1,] 3 -11 16
[2,] 2 -8 8
[3,] 1 -3 2
> svd(a)
$d
[1] 22.9607876 2.1782253 0.2399339
OK!
$u
[,1]
[,2]
[,3]
[1,] -0.8544093 0.4892724 0.1749207
[2,] -0.4963077 -0.6688083 -0.5535107
[3,] -0.1538291 -0.5597392 0.8142657
$v
[,1]
[,2]
[,3]
[1,] -0.1615656 -0.1971966 0.96695919
[2,] 0.6023509 0.7564359 0.25490812
[3,] -0.7817096 0.6236331 -0.00343243
Matrix Determinant
>a
[,1] [,2] [,3]
[1,] 3 -11 16
[2,] 2 -8 8
[3,] 1 -3 2
> det(a)
[1] 12
8. OPENING AND SAVING AN R SCRIPT
Typically we do not work on R console. Instead, we program everything on a script, save
our program, and then run the program.
First, let us clear the console by going to R->Misc->Remove all objects.
Alternatively, you can type:
> rm(list=ls(all=TRUE))
Open a new script by going to R->File->New Script
Type your R code:
You can create a folder (For Example C:\Program Files\R\my codes\MAN656) for your
MAN 656 files:
Now run your code by going to R->edit->run all (script window must be active to see this
menu). Here is the result: