```r
satData <- read.csv("http://r-marketing.r-forge.r-project.org/data/rintro-chapter2.csv")

# examine the data
head(satData)
str(satData)
summary(satData)

# convert Segment to a factor variable
satData$Segment <- factor(satData$Segment)
summary(satData)

# Satisfaction by segment
library(ggplot2)
by(satData$iProdSAT, satData$Segment, mean_se, mult=1.96)


###### EXAMPLE OF PLOTTING

### first aggregate the data we want to plot
# ... there are other ways to do this, but this illustrates data frame manipulation

# get the mean and standard errors
prod.sat.seg <- aggregate(satData$iProdSAT, list(satData$Segment), mean_se, mult=1.96)
str(prod.sat.seg)

# coerce those to a nice data frame
prod.sat.seg <- data.frame(prod.sat.seg$Group.1, lapply(data.frame(prod.sat.seg$x), unlist))
prod.sat.seg

# and label the columns to be more readable
names(prod.sat.seg) <- c("Segment", "average", "lowerCI", "upperCI")
prod.sat.seg

### the plot itself ...

# now plot the interquartile range
p <- ggplot(data=prod.sat.seg, 
            aes(x=Segment, y=average, ymax=upperCI, ymin=lowerCI)) +
     geom_point() +
     geom_errorbar()

p

# color the points and make them larger
p <- ggplot(data=prod.sat.seg, 
            aes(x=Segment, y=average, ymax=upperCI, ymin=lowerCI)) +
     geom_point(aes(color=Segment), size=3) +      # <=======
     geom_errorbar() 

p

# color the error bars and make them narrower
p <- ggplot(data=prod.sat.seg, 
            aes(x=Segment, y=average, ymax=upperCI, ymin=lowerCI)) +
     geom_point(aes(color=Segment), size=3) +
     geom_errorbar(aes(color=Segment), width=0.3)      # <=======

p

# adjust the Y axis range
p <- ggplot(data=prod.sat.seg, 
            aes(x=Segment, y=average, ymax=upperCI, ymin=lowerCI)) +
     geom_point(aes(color=Segment), size=3) +
     geom_errorbar(aes(color=Segment), width=0.3) +
     coord_cartesian(ylim=c(1, 5))    # <========

p

# add some titles to be more readable
p <- ggplot(data=prod.sat.seg, 
            aes(x=Segment, y=average, ymax=upperCI, ymin=lowerCI)) +
     geom_point(aes(color=Segment), size=3) +
     geom_errorbar(aes(color=Segment), width=0.3) +
     coord_cartesian(ylim=c(1, 5)) +
     ggtitle("Average Sat and Confidence Interval by Segment") +    # <=====
     ylab("Mean satisfaction and 95% CI")

p


########## CORRELATIONS

# correlation matrix
library(corrplot)
corrplot(cor(satData[ , -3]))

# tinker with the plot
corrplot.mixed(cor(satData[ , -3]))


#### ADVANCED MODELING. EXAMPLE: SAT/REC STRUCTURAL MODEL

# define a structural model for Satisfaction and Recommendation
satModel <- "SAT =~ iProdSAT + iSalesSAT
             REC =~ iProdREC + iSalesREC
             REC ~  SAT "

# fit the structural model
library(lavaan)
sat.fit <- cfa(satModel, data=satData)

# look at the fit
summary(sat.fit, fit.measures=TRUE)

# plot the structural model
library(semPlot)
semPaths(sat.fit, what="est", nCharNodes=9, residuals=FALSE)
```