User:Alpha7209/sandbox

getwd setwd("E:/D/NIIT/MBA (IDS)/Decision Analysis") library(ISLR) install.packages("ISLR")# Books library#
 * 1) Tree

install.packages("tree") #To fit Decsion Tree# library(ISLR) library(tree)

car<-read.csv("E:/D/NIIT/MBA (IDS)/Decision Analysis/Carseats.csv") attach(car) head(car) dim(car)


 * 1) Start data Manipulation ##

High<-ifelse(Sales>=8, "Yes", "No")
 * 1) Create a categorical variable based on Sales###

length(High) dim(car)

car<-data.frame(car, High)
 * 1) appends High to Carseats, and now our dataset is ready!!##

car=car[,-1] names(car)


 * 1)  Split  data into testing and training###

set.seed(2) ### every time same random dataset will generate for traing and testing###

ind1<-sample(1:nrow(car), 300) trainDF<-car[ind1,]

ind2<-sample(301:nrow(car)) testDF<-car[ind2, ] dim(trainDF) dim(testDF)

nrow(testDF)

train=sample(1:nrow(car),300) test=-train training_data=car[train,] testing_data=car[test,] testing_High=High[test]

names(car) sapply(car,class) dim(car)
 * 1) fit the tree model using training data####

tree_model=tree(High ~Income+Advertising+Population+age, training_data) plot(tree_model) text(tree_model, pretty=0)

tree_model=tree(High ~Income+Advertising+Population+age, testing_data) plot(tree_model) text(tree_model, pretty=0)


 * 1) Decision#################33

Codes for Decision Analysis

install.packages("rpart") library(rpart) getwd Fitness<-read.csv("E:/D/NIIT/MBA (IDS)/Decision Analysis/fitness.csv")

attach(Fitness)

names(Fitness)

tree_analysis<-rpart(Payornot~Income+Gymvisits+State, data=Fitness) tree_analysis

install.packages("rpart.plot") library(rpart.plot)

rpart.plot(tree_analysis,extra=1)


 * 1) Conjoint

getwd setwd("D:\\Marketing Analytics")
 * 1) Read Data

conjoint<-read.csv("D:\\Marketing Analytics\\Conjoint1.csv") View(conjoint)


 * 1) Attribute levels

I<-c("domestic", "foreign", "22 mpg", "28 mpg", "2DR", "4DR", "$22000", "$18000") I.df<-data.frame(I) I.df

Conj.result<-lm(Rating~Make+mpg+Door+Price, data=conjoint) summary(Conj.result)
 * 1) Run  Conjoint Anlysis with Regression


 * 1) Load library (conjoint)

install.packages("conjoint") library("conjoint") names(conjoint)
 * 1) Get Utility of each attributes

caModel(conjoint[,1],conjoint[,2:5]) Conjoint(conjoint[,1],conjoint[,2:5], I.df)

caTotalUtilities(conjoint[,1],conjoint[,2:5])
 * 1) Total Utilities based on model

caImportance(conjoint[,1],conjoint[,2:5])
 * 1) Attribute importance


 * 1) Neural Network

library(MASS) library(neuralnet) set.seed(42) Dataset<-Boston help(Boston)
 * 1) install.packages("MASS")
 * 2) install.packages("neuralnet")

str(Boston)

attach(Dataset) #We dont have to specify the dataset with the column name instead with this we can directly use the column name

hist(medv)

dim(Dataset)

head(Dataset)

summary(Dataset) apply(Dataset, 2, range)

maxvalue = apply(dataset,2,max) minvalue = apply(dataset,2,min) Dataset<- as.data.frame(scale(Dataset, center=minvalue, scale=maxvalue-minvalue))

ind1<- sample(1:nrow(Dataset), 400) trainDF<- Dataset[ind1,]

ind2<- sample(401:nrow(Dataset)) testDF<- Dataset[ind2,]

dim(trainDF) dim(testDF)

nrow(testDF)

allvars<- colnames(Dataset) predictorvars<-allvars[!allvars %in% "medv"] predictorvars<- paste(predictorvars, collapse= '+')

form = as.formula(paste("medv~", predictorvars, collapse = '+'))

neuralmodel<- neuralnet(formula=form, hidden=c(4,2),linear.output = T, data=trainDF) plot(neuralmodel)

prediction<-compute(neuralmodel,testDF) print(prediction$net.result)



library(MASS) library(nnfor) library(ts)

air.train=window(AirPassengers, end=1958) air.test=window(AirPassengers, start=1958.001)

air.fit.nnetar=nnetar(air.train) air.fcst.nnetar = forecast(air.fit.nnetar)

plot(air.fcst.nnetar)


 * 1) library(lpSolve)


 * 1) Assignment Problem

assign.cost<-matrix(c(13, 4, 7, 6, 1, 11, 5, 4, 6, 7, 2, 8, 1, 3, 5, 9), ncol=4, byrow=TRUE) assign.cost

model1<-lp.assign(assign.cost) model1$solution

model1$objval

assign.cost<-matrix(c(10,4,6,10,12,11,7,7,9,14,13,8,12,14,15,14,16,13,17,17,19,11,17,20,19), ncol=5, byrow=TRUE) assign.cost
 * 1) Assignment Problem 2

model1<-lp.assign(assign.cost) model1$solution

model1$objval

costs<-matrix(c(10,0,20,11,12,7,9,20,0,14,16,18), ncol=4, byrow=TRUE) costs
 * 1) Transportation Problem

row.rhs<-c(20,25,15) #supply row.sign<-rep("<",3)

col.rhs<-c(10,15,15,20) #demand col.sign<-rep(">",4)

model2<-lp.transport(costs,"min",row.sign,row.rhs,col.sign,col.rhs) model2

model2$solution model2$objval

costs<-matrix(c(5,4,3,8,4,3,9,7,5), ncol=3, byrow=TRUE) costs
 * 1) Transportation Problem2

row.rhs<-c(100,300,300) #supply row.sign<-rep("<",3)

col.rhs<-c(300,200,200) #demand col.sign<-rep(">",3)

model3<-lp.transport(costs,"min",row.sign,row.rhs,col.sign,col.rhs) model3

model3$solution model3$objval


 * 1) Assignment Problem 3

assign.cost<-matrix(c(30,27,31,39,0,28,18,28,37,0,33,17,29,41,0,27,18,30,43,0,40,20,27,36,0), ncol=5, byrow=TRUE) assign.cost

model1<-lp.assign(assign.cost) model1$solution

model1$objval

costs<-matrix(c(3,2,7,6,7,5,2,3,2,5,4,5), ncol=4, byrow=TRUE) costs
 * 1) Transportation Problem 3

row.rhs<-c(5000,6000,2500) #supply row.sign<-rep("<",3)

col.rhs<-c(6000,4000,2000,1500) #demand col.sign<-rep(">",4)

model2<-lp.transport(costs,"min",row.sign,row.rhs,col.sign,col.rhs) model2

model2$solution model2$objval