-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintro.r
72 lines (57 loc) · 1.76 KB
/
intro.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# ? Rows for samples
# ? Columns for variables
# Example
x <- c(1, 2, 3)
y <- c(4, 5, 6)
A <- cbind(x, y)
print(A)
# ? Rank of a matrix
# No. of linearly independent rows or columns
# Command: Rank(A) (not rank(A))
library(pracma) # For Rank() function
print("Rank Example")
A <- matrix(c(1, 2, 3, 2, 4, 6, 1, 0, 0),
ncol = 3,
byrow = F
)
print(A)
print(Rank(A))
# ? Identification of Linear Relationships among Attributes
# Null Space of A
# AB = 0 and B != 0
# Nullity of a matrix is the no. of vectors in the null space of A
# The size of the null space of a matrix provides us with the no. of linear relations among the attributes
# The null space vectors B are useful to identify the linear relations among the attributes
# ? Rank Nullity Theorem
# Rank(A) + Nullity(A) = No. of columns of A
# where,
# Rank(A) = No. of independent variables
# Nullity(A) = No. of eqns among the variables
# No. of columns of A = Total no. of variables
# Example
print("Rank Nullity Theorem Example 1")
A <- matrix(c(1, 3, 5, 2, 4, 6), ncol = 2, byrow = F)
print(A)
columns <- ncol(A)
rank <- Rank(A)
nullity <- columns - rank
# paste() is used to concatenate strings
print(paste("Columns: ", columns)) # 2
print(paste("Rank: ", rank)) # 2
print(paste("Nullity: ", nullity)) # 0
# Nullity is 0, so all attributes are linearly independent
# Another example
print("Rank Nullity Theorem Example 2")
A <- matrix(c(1, 2, 3, 2, 4, 6, 0, 0, 1),
ncol = 3,
byrow = F
)
print(A)
columns <- ncol(A) # No. of columns
rank <- Rank(A)
nullity <- columns - rank
print(paste("Columns: ", columns)) # 3
print(paste("Rank: ", rank)) # 2
print(paste("Nullity: ", nullity)) # 1
# Since nullity is 1, there is one linear relation among the attributes
# Thus, we need to identify the null space vector B