##### Generating vector ###################
a=c(1,2,3)
b=c(2,3,4)
b[2]
#### Vector addition, substraction ########
a+b
a-b
2*(b+10)
#### dot product #####
a%*%b
##### Generating Matrix ######
y <- matrix(c(1,2,3,4,9,10),nrow=3,ncol=2)
nrow(y)
ncol(y)
dim(y)
length(y)
x <- matrix(nrow=3,ncol=2)
x[1,1] <- 11
x[2,1] <- 22
x[1,2] <- 30
x[2,2] <- 14
x[3,1] <- 3
x[3,2] <- 1
#### Matrix Operation ######
a%*%x
3*y
y+x
a%*%(y-x)
###### Network application ###########
T=rbind(c(0,1,0,0), c(1,0,1,0), c(0,1,0,1), c(0,1,0,0))
T*T #### check
T^2 #### check
T%*%T ### One-step trasition
T%*%T%*%T ### two-step trasition
####### Matrix Indexing ###########
y[1,2]
y[1, ]
y[1:2, ]
####### Filtering in Matrix ###########
y[y[,2] >= 9,]
y[y[,1] >= 3,]
y[y[,1] >= 2 & y[,2] > 5,]
y[y[,1] > 2 & y[,2] > 5,]
###### Apply Function ########
apply(y,1,sum)
apply(y,2,mean)
f <- function(x) {
sum(x)/(max(x)-min(x))
apply(y,1,f)
######### Adding and Deleting Matrix Rows and Columns ##########
cbind(y,a)
rbind(y, c(10,12))
y[-1,]
########### Inverse ## Transpose ##########
A=matrix(c(1,1,3,2,3,4,4,5,5),3,3)
det(A)
sum(diag(A))
eigen(A)
t(A)
A%*%t(A)
B=solve(A)
B
#### solving linear equation ######
solve(matrix(c(3,2,2,4), 2, 2))%*%c(80,80)
solve(matrix(c(4,-2,3,-1), 2, 2))%*%c(4,0)
########### Higher-Dimensional Arrays ############
### Data on Quarterly Advertising Expenses and Quarterly Net Sales for Trent from
Dec 2003 to Mar 2008.
Adv_exp=c(4.3, 2.7, 4.59,4.54,8.87,3.17,7.44,8.33,9,5.08,11.18,10.18,11.03,5.67)
Personnel_cost=c(2.74, 3.27, 3.3, 3.41, 3.85, 4.1, 4.14, 4.89, 5.22, 6.02, 6.64,
7.5, 7.11, 7.19)
Sales=c(40.13, 42.02, 40.35, 46.89, 54.1, 70.13, 63.37, 74.01, 86.63, 95.17, 90.63,
105.09, 120.29, 121.83)
Sales_data=cbind(Sales,Adv_exp,Personnel_cost)
cor(Sales_data)
plot(Adv_exp, Sales, col='blue')
plot(Adv_exp, Sales)
plot(Sales, Personnel_cost)
Reg_1=lm(Sales~Adv_exp)
summary(Reg_1)
plot(resid$reg_1)
plot(Adv_exp, Sales, col='blue')
abline ( Reg_1 , col='red') # plot fitted linear regression line
res_1=Sales-fitted(Reg_1)
plot(res_1)
qqnorm (res_1 )
qqline (res_1 )
##############################################
plot(Personnel_cost, Sales, col='blue')
Reg_2=lm(Sales~Personnel_cost)
summary(Reg_2)
plot(Personnel_cost, Sales, col='blue')
abline( Reg_2 , col='red') # plot fitted linear regression line
res_2=Sales-fitted(Reg_2)
plot(res_2)
qqnorm (res_2 )
qqline (res_2 )
#########################################################
Reg_3=lm(Sales~Personnel_cost+Adv_exp)
summary(Reg_3)
res_3=Sales-fitted(Reg_3)
plot(res_3)
qqnorm (res_3 )
qqline (res_3 )
###################### Brand market share ###################
S_0=c(0.3, 0.7)
C=matrix(c(0.8,0.2,0.3,0.7),2,2)
S_1=C%*%S_0
S_1
S_2=C%*%S_1
S_2
S_3=C%*%S_2
S_3
S_4=C%*%S_3
S_4
S_5=C%*%S_4
S_5
S_6=C%*%S_5
S_6
S_7=C%*%S_6
S_7
########### Equlibrium #############
eigen(C)
eigen(C)$vectors[,1]/sum(eigen(C)$vectors[,1])
######### General solution ############
S_0=c(0.1,0.2,0.7)
C=rbind(c(0.65,0.05,0.1),c(0.2, 0.9,0.1), c(0.15,0.05,0.8))
I=diag(c(1,1,1))
D=(C-I)[-3,] # step I
D=rbind(D, c(1,1,1))
x=c(0,0,1) # step II
solve(D)%*%x # step III
i=1
n=40
S_0=C%*%S_0
while ( i < n ) {
S_0=C%*%S_0
print(S_0)
i=i+1
}