Skip to content

Instantly share code, notes, and snippets.

View Harshit1694's full-sized avatar

HARSHIT GUPTA Harshit1694

View GitHub Profile
#Market Share after one month
Current_state<-c(0.55,0.45)
steps<-1
finalState<-Current_state*disc_trans^steps #using power operator
finalState
#Market Share after two month
Current_state<-c(0.55,0.45)
steps<-2
finalState<-Current_state*disc_trans^steps #using power operator
install.packages("markovchain")
install.packages("diagram")
library(markovchain)
library(diagram)
# Creating a transition matrix
trans_mat <- matrix(c(0.7,0.3,0.1,0.9),nrow = 2, byrow = TRUE)
trans_mat
# create the Discrete Time Markov Chain
#Bayes Theorem
cancer <- sample(c('No','Yes'), size=1000, replace=TRUE, prob=c(0.99852,0.00148))
test <- rep(NA, 1000) # creating a dummy variable
test[cancer=='No'] <- sample(c('Negative','Positive'), size=sum(cancer=='No'), replace=TRUE, prob=c(0.99,0.01))
test[cancer=='Yes'] <- sample(c('Negative','Positive'), size=sum(cancer=='Yes'), replace=TRUE, prob=c(0.07, 0.93))
P_cancer_and_pos<-0.00148*0.93
P_no_cancer_and_pos<-0.99852*0.01
P_Accident_who_follow_Traffic_Rule<-50
P_who_follow_Traffic_Rule=50+2000
Conditional_Probability=(P_Accident_who_follow_Traffic_Rule/P_who_follow_Traffic_Rule)
Conditional_Probability
f <- function(toss=1){
x <- sample(1:2, size=toss, replace=TRUE)
y <- sample(1:2, size=toss, replace=TRUE)
return(cbind(x,y))
}
set.seed(2500)
toss_times <- as.data.frame(f(2000))
library(plyr)
install.packages("lpSolve")
library(lpSolve)
#Setting the coefficients of decision variables
objective.in=c(25,20)
#Constraint Matrix
const.mat=matrix(c(20,12,5,5),nrow = 2,byrow = T)
#Forecast of Arima Model
fts <- forecast(arimats, level = c(95))
autoplot(fts)
#Fitting the model
#Linear model
autoplot(tsdata) + geom_smooth(method="lm")+ labs(x ="Date", y = "Passenger numbers (1000's)", title="Air Passengers data")
#ARIMA Model
arimats <- auto.arima(tsdata)
arimats
ggtsdiag(arimats)
#Testing the stationarity of the data
#Augmented Dickey-Fuller Test
adf.test(tsdata)
#Autocorrelation test
autoplot(acf(tsdata,plot=FALSE))+ labs(title="Correlogram of Air Passengers data")
tsdata_decom$random
autoplot(acf(tsdata_decom$random[7:138],plot=FALSE))+ labs(title="Correlogram of Air Passengers Random Component")
#Decomposing the data into its trend, seasonal, and random error components
tsdata_decom <- decompose(tsdata, type = "multiplicative")
plot(tsdata_decom)