#::::::::::::::::: PSY9140 SEM course Day 4 :::::::::::::::::::::::::::::###

################### (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 "data3.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/data3.csv")

# descriptive statistics for all variables in "df1" by using "describe" from the "psych" package
describe(df1)

# Model specification
model <- "# Specify the model 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, modindices = TRUE)


################### (2) Exercise 2 #######################################

# load the datafile "data4.csv" as a dataframe with the name "df1" by using the "read.csv" command
df2 <- read.csv("https://www.sv.uio.no/psi/personer/vit/tilmanns/psy9140/data4.csv")

# descriptive statistics for all variables in "df2" by using "describe" from the "psych" package
describe(df2)

# Specify the model
model <- "# Latent factors diet
            diet1 =~ diet11 + diet12 + diet13 + diet14
            diet2 =~ diet21 + diet22 + diet23 + diet24
          # Latent factors body
            body1 =~ body11 + body12 + body13 + body14 + body15
            body2 =~ body21 + body22 + body23 + body24 + body25
          # Covariances
            diet1 + diet2  ~~ body1 + body2
            diet1 ~~ diet2
            body1 ~~ body2"

# Run the model
fit <- sem(model, missing = "ML", meanstructure = TRUE, data = df2)

# Increase the number of rows printed in the output
options(max.print=10000)

# Providing output for the model
summary(fit,  fit.measures = TRUE, standardized=TRUE, modindices = TRUE)


################### (3) Exercise 3 #######################################

# Specify the model in the next line
model <- "# Specify the model her"

# Running the model
fit <- growth(model, missing = "ML", meanstructure = TRUE, data = df1)

# Providing output for the model
summary(fit, fit.measures = TRUE, standardized=TRUE)


################### (4) Exercise 4 #####################################

# Specify the model in the next line
model <- "# LGC specification
            i =~ 1*body1 + 1*body2 + 1*body3
            s =~ 0*body1 + 2*body2 + 7*body3
          # Covariance between i and s
            i ~~ s
          # Regression
            i + s ~ girl"

# Running the model
fit <- growth(model, missing = "ML", meanstructure = TRUE, data = df1)

# Providing output for the model
summary(fit, fit.measures = TRUE, standardized=TRUE)


################### (5) Exercise 5 #######################################

# Specify the model in the next line
model <- "# LGC specification
            i_b =~ 1*body1 + 1*body2 + 1*body3
            s_b =~ 0*body1 + 2*body2 + 7*body3
            i_d =~ 1*diet1 + 1*diet2 + 1*diet3
            s_d =~ 0*diet1 + 2*diet2 + 7*diet3
          # Covariance between i and s
            i_b ~~ s_b
            i_d ~~ s_d
            i_d ~~ i_b
            s_d ~~ s_b
            i_d ~~ s_b
            i_b ~~ s_d"

# Running the model
fit <- growth(model, missing = "ML", meanstructure = TRUE, data = df1)

# Providing output for the model
summary(fit, fit.measures = TRUE, standardized=TRUE)


################### (6) Exercise 6 #######################################

# load the datafile "data5.csv" as a dataframe with the name "df3" by using the "read.csv" command
df3 <- read.csv("https://www.sv.uio.no/psi/personer/vit/tilmanns/psy9140/data5.csv")

# descriptive statistics for all variables in "df1" by using "describe" from the "psych" package
describe(df3)

# Specify the model in the next line
model <- "# LGC specification
            i =~ 1*body1 + 1*body2 + 1*body3 + 1*body4
            s =~ 0*body1 + 2*body2 + 7*body3 + 13*body4" 

# Running the model
fit <- growth(model, missing = "ML", meanstructure = TRUE, data = df3)

# Providing output for the model
summary(fit, fit.measures = TRUE, standardized=TRUE)










