Skip to content

Instantly share code, notes, and snippets.

View mihaiconstantin's full-sized avatar
📑
Thesis Writing

Mihai Constantin mihaiconstantin

📑
Thesis Writing
View GitHub Profile
@mihaiconstantin
mihaiconstantin / latent-growth-curve-change.R
Last active January 15, 2024 13:54
The Latent Growth Curve Model and Change Over Time
# Libraries.
library(lavaan)
library(ggplot2)
library(patchwork)
# If not installed, then install as follows:
install.packages(c("lavaan", "ggplot2", "patchwork"))
# Model 1.
#
@mihaiconstantin
mihaiconstantin / sem-phantoms.R
Last active January 10, 2024 14:52
Adding phantom latent variables in SEM in `lavaan`.
# Phantom latent variables.
# Load the packages.
library(lavaan)
library(semPlot)
# Load data.
data(HolzingerSwineford1939)
# Store the data.
@mihaiconstantin
mihaiconstantin / sem-estimation.R
Last active January 10, 2024 14:52
A step-by-step tutorial for fitting SEM models in base `R`.
# Load libraries.
library(lavaan)
# #region Helpers.
# These functions exist to make it more convenient for you to carry out the
# model specification without dealing with unintuitive `R` syntax and naming
# conventions. You can ignore this section and focus on the steps that follow
# (i.e., the actual model specification and estimation).
@mihaiconstantin
mihaiconstantin / warnings.R
Last active August 3, 2023 12:57
Stop `R` warnings from printing and capture them in a variable for later handling.
# Please run the code top to bottom and follow the comments.
# Task function with errors or warnings.
naughty_task <- function(error = TRUE, warning = FALSE) {
# If warning.
if (warning) {
warning("Warning thrown.")
}
# If error.
@mihaiconstantin
mihaiconstantin / cran-downloads-trend.R
Last active May 18, 2024 19:27
Daily downloads trend for `R` packages published on `CRAN`
# A function to quickly grasp the `CRAN` daily downloads.
plot_cran_downloads_trend <- function(package, x_axis_frequency = "3 months") {
# Packages required.
packages <- c("cranlogs", "mgcv", "curl", "ggplot2", "scales")
# Load required packages.
sapply(packages, function(pkg) {
# Require the package.
status <- suppressPackageStartupMessages(require(pkg, character.only = TRUE))
@mihaiconstantin
mihaiconstantin / windows-apps-on-mac.md
Last active March 29, 2022 04:44
Installing and running Windows applications on M1 Macs

Goal

Install and run the Windows version of LatentGOLD 6.0 on a Mac device (Intel or Apple Silicon).

Steps

1. Install homebrew

@mihaiconstantin
mihaiconstantin / monotone_spline.R
Created April 12, 2021 18:31 — forked from willtownes/monotone_spline.R
monotone splines using package mgcv
library(mgcv)
#library(modules) #devtools::install_github(klmr/modules)
#mgcv<-import_package("mgcv")
mspline<-function(x,y,k=10,lower=NA,upper=NA){
#fits a monotonic spline to data
#small values of k= more smoothing (flatter curves)
#large values of k= more flexible (wiggly curves)
#k is related to effective degrees of freedom and number of knots
#use unconstrained gam to get rough parameter estimates
@mihaiconstantin
mihaiconstantin / CodeExample.R
Last active August 5, 2019 06:42
Code example for StackOverflow question on `foreach` and `R6` classes (https://stackoverflow.com/questions/57349958).
# # # Scope of the issue:
#
# - without the following line `object$parent_env <- environment()` (i.e., line 54 below), it throws an error:
# - `Error in { : task 1 failed - "object 'Work' not found"`.
#
# - I would like to know:
# 1. what are some potential pitfalls when assigning the `parent_env` inside `foreach`
# 2. why does it work in the first place
# Minimal reproducible example: