-
Notifications
You must be signed in to change notification settings - Fork 1
Home
This wiki answers some common questions about semproducible.
Use the save_code()
function:
code <- semproducible(...)
save_code(code, "my_file.r")
If my_file.r
already exists, the function will not continue. You need to explicitly add the overwrite=TRUE
parameter to overwrite existing files:
save_code(code, "my_file.r", overwrite=TRUE)
When you want to generate code for a data frame, the default behavior is that semproducible requires you to specify which numeric columns that will be used in the covariance matrix.
Look at the iris
dataset, for example:
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
The column Species
is a factor, which means that you will get an error since semproducible require numeric columns.
Use drop_non_numeric=TRUE
to automatically drop any column that is not numeric:
semproducible(iris, drop_non_numeric=TRUE)
The following message is then displayed:
Dropped 1 non-numeric column(s): Species
If you have a large data frame you want to fit on a page (e.g. journal article or appendix), you can control the physical width of the generated code. For example:
- Set
vars_per_line=2
to use two variables/values per line - Set
digits=4
to round numbers into four decimals (the default behavior is to use as many decimals as your current R session)
semproducible(iris, drop_non_numeric=TRUE, vars_per_line=2, digits=4)
Output:
[...]
# Covariance matrix.
cov_mat <- tribble(~Sepal.Length, ~Sepal.Width,
~Petal.Length, ~Petal.Width,
0.6857, -0.0424,
1.2743, 0.5163,
-0.0424, 0.19,
-0.3297, -0.1216,
1.2743, -0.3297,
3.1163, 1.2956,
0.5163, -0.1216,
1.2956, 0.581)
[...]
Yes, semproducible can be used together with pipes (the %>%
operator):
library(tidyverse)
iris %>%
select(Sepal.Length, Sepal.Width, Petal.Length) %>%
semproducible(formula = "Sepal.Length ~ Sepal.Width + Petal.Length") %>%
cat()
Or directly after a lavaan object:
sem(model, data = PoliticalDemocracy) %>%
semproducible()
Use the eval=TRUE
argument to run and evaluate the code automatically.
code <- semproducible(iris[, 1:4], formula="Sepal.Length ~ Sepal.Width + Petal.Length", eval=TRUE)
If the code fails, you will get an error message. If the code runs successfully, semproducible will simply inform you of so and return the code.
Note: semproducible does not evaluate model fit or model convergence!
The generated code will produce similar result to your original lavaan model. This is because the generated code creates a covariance matrix of your raw data, and the modeling is based on the covariance matrix.
You can control the similarity by increasing the digits
argument. The higher the number, the more similar the generated result will be. However, it is likely that the value is at its maximum already.