## A.2 Customer Satisfaction Survey Data from Airline Company

# Create a matrix of factor loadings This pattern
# is called bifactor because it has a general
# factor for separate components.  For example,
# 'Ease of making reservation' has general factor
# outcome variables are formed as combinations of
# these general and specific factors

# Ticketing
.33, .58, .00, .00,  # Ease of making reservation
.35, .55, .00, .00,  # Availability of preferred seats
.30, .52, .00, .00,  # Variety of flight options
.40, .50, .00, .00,  # Ticket prices
# Aircraft
.50, .00, .55, .00,  # Seat comfort
.41, .00, .51, .00,  # Roominess of seat area
.45, .00, .57, .00,  # Availability of Overhead
.32, .00, .54, .00,  # Cleanliness of aircraft
# Service
.35, .00, .00, .50,  # Courtesy of flight attendant
.38, .00, .00, .57,  # Friendliness
.60, .00, .00, .50,  # Helpfulness
.52, .00, .00, .58,  # Food and drinks
# General
.43, .10, .30, .30,  # Overall satisfaction
.35, .50, .40, .20,  # Purchase again
.25, .50, .50, .20), # Willingness to recommend
nrow=15,ncol=4, byrow=TRUE)

# Matrix multiplication produces the correlation
# matrix except for the diagonal
# Diagonal set to ones
diag(cor_matrix) <- 1

# use the mvtnorm package to randomly generate a
# data set with a given correlation pattern

library(mvtnorm)
# mean vectors of the 3 airline companies
mu1 = c(5, 6, 5, 6, 7, 8, 6, 7, 5, 5, 5, 5, 6, 6, 6)
mu2 = c(3, 3, 2, 3, 5, 4, 5, 6, 8, 8, 8, 8, 3, 3, 3)
mu3 = c(2, 2, 2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8)

# set random seed
set.seed(123456)
# respondent ID
resp.id <- 1:1000

library(MASS)
rating1 <- mvrnorm(length(resp.id), mu = mu1, Sigma = cor_matrix)
rating2 <- mvrnorm(length(resp.id), mu = mu2, Sigma = cor_matrix)
rating3 <- mvrnorm(length(resp.id), mu = mu3, Sigma = cor_matrix)

# truncates scale to be between 1 and 9
rating1[rating1 > 9] <- 9
rating1[rating1 < 1] <- 1
rating2[rating2 > 9] <- 9
rating2[rating2 < 1] <- 1
rating3[rating3 > 9] <- 9
rating3[rating3 < 1] <- 1

# Round to single digit
rating1 <- data.frame(round(rating1, 0))
rating2 <- data.frame(round(rating2, 0))
rating3 <- data.frame(round(rating3, 0))
rating1$ID <- resp.id rating2$ID <- resp.id
rating3$ID <- resp.id rating1$Airline <- rep("AirlineCo.1", length(resp.id))
rating2$Airline <- rep("AirlineCo.2", length(resp.id)) rating3$Airline <- rep("AirlineCo.3", length(resp.id))
rating <- rbind(rating1, rating2, rating3)

# assign names to the variables in the data frame
names(rating) <- c("Easy_Reservation", "Preferred_Seats",
"Flight_Options", "Ticket_Prices", "Seat_Comfort",
"Airline")