#::::::::::::::::: PSY9140 SEM course Day 3 :::::::::::::::::::::::::::::###

################### (0) Preparations #######################################
# installing packages; has just to be done once.
# install.packages("lavaan")
# install.packages("psych")
# install.packages("lavaanPlot")
# install.packages("semPlot")
# install.packages("semTable")
# install.packages("ggcorrplot")

# load packages. Has to be done each time R is started.
library(lavaan)
library(psych)
library(lavaanPlot)
library(semPlot)
library(semTable)
library(ggcorrplot)


################### (1) Exercise 1 #######################################

# load the datafile "data1.csv" as a dataframe with the name "df1" by using the "read.csv" command
df1 <- read.csv("https://www.sv.uio.no/psi/personer/vit/tilmanns/psy9140/data1.csv")

# descriptive statistics for all variables in "df1" by using "describe" from the "psych" package
describe(df1)

# Model specification
model <- "diet ~ dep
          diet ~ body
          body ~~ dep"

# Running the model
fit <- sem(model, missing = "ML", meanstructure = TRUE, data = df1)

# Providing output for the model
summary(fit,  fit.measures = TRUE, standardized=TRUE)


################### (2) Exercise 2 #######################################

# Specify the model in the next line
model <- "# Write model specification in here"

# Running the model
fit <- sem(model, missing = "ML", meanstructure = TRUE, data = df1)

# Providing output for the model
summary(fit,  fit.measures = TRUE, standardized=TRUE)


################### (3) Exercise 3 #######################################

# load the datafile "data2.csv" as a dataframe with the name "df2" by using the "read.csv" command
df2 <- read.csv("https://www.sv.uio.no/psi/personer/vit/tilmanns/psy9140/data2.csv")

# descriptive statistics for all variables in "df2"
describe(df2)

# Specify the model in the next line
model <- "# Write model specification in here"

# Running the model
fit <- sem(model, missing = "ML", meanstructure = TRUE, data = df2)

# Providing output for the model
summary(fit,  fit.measures = TRUE, standardized=TRUE, modindices = TRUE)


################### (4) Exercise 4 #####################################

model <- "# CFA
            body =~ body1 + body2 + body3 + body4 + body6
            dep =~ dep1 + dep2 + dep3 + dep4 + dep5 + dep6
            diet =~ diet1 + diet2 + diet3 + diet4
          # Structural models
            diet ~ dep
            diet ~ body
            dep ~ body"

# Running the model
fit <- sem(model, missing = "ML", meanstructure = TRUE, data = df2)

# Providing output for the model
summary(fit, fit.measures = TRUE, standardized=TRUE)


################### (5) Exercise 5 #######################################

model <- "# CFA
            body =~ body1 + body2 + body3 + body4 + body6
            dep =~ dep1 + dep2 + dep3 + dep4 + dep5 + dep6
            diet =~ diet1 + diet2 + diet3 + diet4
          # Structural models
            diet ~ dep
            diet ~ body
            dep ~ body"

# Running the model
fit <- sem(model, missing = "ML", meanstructure = TRUE, data = df2, group = "girl")

# Providing output for the model
summary(fit, fit.measures = TRUE, standardized=TRUE)


################### (6) Exercise 6 #######################################

# Specify the model in the next line
model <- "# Write model specification in here"

# Running the model
fit <- sem(model, missing = "ML", meanstructure = TRUE, data = df2, 
           group = "girl", group.equal = c("loadings"))

# Providing output for the model
summary(fit, fit.measures = TRUE, standardized=TRUE)


################### (7) Exercise 7 #######################################

model <- "# CFA
            body =~ body1 + body2 + body3 + body4 + body6
            diet =~ diet1 + diet2 + diet3 + diet4
          # Structural models
            diet ~ body"

# Running the model
fit <- sem(model, missing = "ML", meanstructure = TRUE, data = df2, 
            group = "girl", group.equal = c("loadings") )


# Providing output for the model
summary(fit, fit.measures = TRUE, standardized=TRUE)


################### (8) Exercise 8 #######################################

# installing package "foreign (has just to be done once).
# install.packages("foreign")

# Load package "foreign"
library(foreign)

# Load SPSS file as data frame
df_cosmetics <- read.spss("https://www.sv.uio.no/psi/personer/vit/tilmanns/psy9140/cosmetics.sav",
                          use.value.labels=FALSE, to.data.frame=TRUE)

# descriptive statistics for all variables in "df_cosmetics"
describe(df_cosmetics)

