### Install IRTorch from GitHub Source: https://irtorch.readthedocs.io/en/latest/_sources/installation.rst.txt Install the latest version of IRTorch directly from its GitHub repository. ```bash pip install git+https://github.com/joakimwallmark/irtorch.git ``` -------------------------------- ### Install IRTorch from PyPI Source: https://irtorch.readthedocs.io/en/latest/_sources/installation.rst.txt Use this command to install the IRTorch package from the Python Package Index. ```bash pip install irtorch ``` -------------------------------- ### Install JupyterLab Source: https://irtorch.readthedocs.io/en/latest/_sources/installation.rst.txt Install JupyterLab, a web-based interactive development environment for working with Jupyter notebooks. ```bash pip install jupyterlab ``` -------------------------------- ### Verify IRTorch Installation (Local) Source: https://irtorch.readthedocs.io/en/latest/_sources/installation.rst.txt Import the IRTorch package in a new notebook to confirm successful installation. ```python import irtorch print("IRTorch successfully installed") ``` -------------------------------- ### Install IRTorch in Cloud Environment (e.g., Google Colab) Source: https://irtorch.readthedocs.io/en/latest/_sources/installation.rst.txt Install IRTorch within a cloud-based Jupyter notebook environment like Google Colab using pip. ```bash !pip install irtorch ``` -------------------------------- ### Start JupyterLab Session Source: https://irtorch.readthedocs.io/en/latest/_sources/installation.rst.txt Run this command in your terminal to start a JupyterLab session. ```bash jupyter lab ``` -------------------------------- ### Instantiate and Fit GradedResponse Model Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Example of how to instantiate a GradedResponse model with provided data and fit it using the MML estimation algorithm. ```python >>> from irtorch.models import GradedResponse >>> from irtorch.estimation_algorithms import MML >>> from irtorch.load_dataset import swedish_national_mathematics_1 >>> data = swedish_national_mathematics_1() >>> model = GradedResponse(data) >>> model.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### Set Starting Theta for Bit Score Computation Source: https://irtorch.readthedocs.io/en/latest/scales.html Manually sets the starting theta scores for the bit scale computation using the `set_start_theta` method. This allows for fine-tuning the bit score calculation, especially when the default approximation is not suitable. ```python >>> bit.set_start_theta(start_theta) ``` -------------------------------- ### Load Libraries for IRTorch Source: https://irtorch.readthedocs.io/en/latest/notebooks/swedish_sat.html Imports essential libraries for data manipulation and IRT modeling. Ensure these are installed before running. ```python import copy import torch import irtorch from irtorch.models import NestedLogit, ThreeParameterLogistic from irtorch.estimation_algorithms import MML ``` -------------------------------- ### Create and Convert Example DataFrame to Tensor Source: https://irtorch.readthedocs.io/en/latest/notebooks/national_mathematics.html Generates a pandas DataFrame with random integer data and NaN values, then converts it to a PyTorch tensor. This demonstrates the required data format for IRTorch. ```python np.random.seed(42) example_df = pd.DataFrame({f"Item {i}": np.random.randint(0, 5, 10) for i in range(1, 6)}) missing_indices = np.random.choice(example_df.index, size=10, replace=False) for col in example_df.columns: example_df.loc[np.random.choice(missing_indices, size=2, replace=False), col] = np.nan example_tensor = torch.tensor(example_df.to_numpy()) print(example_tensor) ``` -------------------------------- ### set_start_theta Source: https://irtorch.readthedocs.io/en/latest/scales.html Sets the starting theta scores for the bit scale computation. ```APIDOC ## set_start_theta ### Description Sets the starting theta scores for the bit scale computation. ### Parameters #### Path Parameters - **start_theta** (torch.Tensor) - The starting theta scores for the bit scale computation. ``` -------------------------------- ### irtorch.load_dataset.swedish_sat_quantitative Source: https://irtorch.readthedocs.io/en/latest/datasets.html Load a sample from the quantitative part Swedish SAT dataset and the correct item responses. The correct item responses start from one, and thus a 1 corresponds to a response of 0 in the data. Returns a tuple containing the loaded dataset and the correct item responses. ```APIDOC ## irtorch.load_dataset.swedish_sat_quantitative ### Description Load a sample from the quantitative part Swedish SAT dataset and the correct item responses. The correct item responses start from one, and thus a 1 corresponds to a response of 0 in the data. ### Returns A tuple containing the loaded dataset and the correct item responses. ### Return type tuple (torch.Tensor, list[int]) ``` -------------------------------- ### Initialize and Fit SurprisalSpline Model Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Instantiate the SurprisalSpline model with provided data and fit it using the MML estimation algorithm. Ensure data is loaded correctly before fitting. ```python from irtorch.models import SurprisalSpline from irtorch.estimation_algorithms import MML from irtorch.load_dataset import swedish_national_mathematics_1 data = swedish_national_mathematics_1() model = SurprisalSpline(data) model.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### Initialize and Fit NestedLogit Model Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Demonstrates how to initialize a NestedLogit model with a ThreeParameterLogistic correct response model and fit it using the MML algorithm. Requires data and correct response indicators. ```python from irtorch.models import NestedLogit, ThreeParameterLogistic from irtorch.estimation_algorithms import MML from irtorch.load_dataset import swedish_sat_quantitative data, correct_responses = swedish_sat_quantitative() model_3pl = ThreeParameterLogistic(items = data.shape[1]) model_nested = NestedLogit(correct_responses, model_3pl, data=data) model_nested.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### Get Item Parameters in IRT Format Source: https://irtorch.readthedocs.io/en/latest/notebooks/national_mathematics.html Retrieve estimated item parameters for parametric IRT models. Use `irt_format=True` to get parameters in the traditional IRT format. Zeros are expected for items with fewer than 4 points. ```python gpc.item_parameters(irt_format=True) ``` -------------------------------- ### latent_credible_interval Source: https://irtorch.readthedocs.io/en/latest/estimation_algorithms.html Get the credible interval for the latent variables. ```APIDOC ## latent_credible_interval ### Description Get the credible interval for the latent variables. ### Parameters #### Path Parameters - **input_data** (torch.Tensor) - Required - The input data. - **alpha** (float, optional) - Optional - The significance level. (default is 0.05) ### Returns - **tuple[torch.Tensor, torch.Tensor, torch.Tensor]** - The lower bound, mean, and upper bound of the credible interval. ``` -------------------------------- ### RationalQuadraticSpline Initialization Source: https://irtorch.readthedocs.io/en/latest/torch_modules.html Initializes a RationalQuadraticSpline module with specified parameters for binning and bounds. This module implements the spline transformation. ```python RationalQuadraticSpline(variables=10, num_bins=64, lower_input_bound=0.0, upper_input_bound=1.0) ``` -------------------------------- ### Initialize Generalized Partial Credit Model Source: https://irtorch.readthedocs.io/en/latest/notebooks/national_mathematics.html Initializes a Generalized Partial Credit (GPC) model using provided data. Ensure the 'data_math' variable is loaded. ```python irtorch.set_seed(42) gpc = GeneralizedPartialCredit(data=data_math) ``` -------------------------------- ### theta_scores Source: https://irtorch.readthedocs.io/en/latest/estimation_algorithms.html Get the latent scores from an input dataset using the encoder. ```APIDOC ## theta_scores ### Description Get the latent scores from an input dataset using the encoder. ### Parameters #### Path Parameters - **data** (torch.Tensor) - Required - A 2D tensor with test data. Columns are items and rows are respondents. ### Returns - **torch.Tensor** - A 2D tensor of latent scores. Rows are respondents and columns are latent variables. ``` -------------------------------- ### Fit MonotoneBSpline Model Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Demonstrates how to instantiate and fit the MonotoneBSpline IRT model using sample data and the MML estimation algorithm. Ensure necessary modules are imported. ```python >>> from irtorch.models import MonotoneBSpline >>> from irtorch.estimation_algorithms import MML >>> from irtorch.load_dataset import swedish_national_mathematics_1 >>> data = swedish_national_mathematics_1() >>> model = MonotoneBSpline(data) >>> model.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### latent_mean_se Source: https://irtorch.readthedocs.io/en/latest/estimation_algorithms.html Get the posterior mean and standard error of the latent variables. ```APIDOC ## latent_mean_se ### Description Get the posterior mean and standard error of the latent variables. ### Parameters #### Path Parameters - **input_data** (torch.Tensor) - Required - The input data. ### Returns - **tuple[torch.Tensor, torch.Tensor]** - The posterior mean and standard error of the latent variables. ``` -------------------------------- ### BaseIRTModel Methods Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Provides documentation for the core methods available in the BaseIRTModel class, serving as the foundation for all IRT models. ```APIDOC ## BaseIRTModel Methods ### Description This section details the methods available in the `BaseIRTModel` class, which is the base class for all IRT models in the library. These methods cover model manipulation, evaluation, and parameter handling. ### Methods - `add_scale_transformation()` - `detach_scale_transformations()` - `evaluate` - `expected_item_score_gradients()` - `expected_scores()` - `fit()` - `forward()` - `information()` - `inverse_transform_theta()` - `item_probabilities()` - `item_theta_relationship_directions()` - `latent_scores()` - `load_model()` - `log_likelihood()` - `plot` - `population_difficulty()` - `population_discrimination()` - `probabilities_from_output()` - `probability_gradients()` - `sample_test_data()` - `save_model()` - `theta_transform_jacobian()` - `transform_theta()` ``` -------------------------------- ### Fit GeneralizedPartialCredit Model Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Shows how to initialize and train a GeneralizedPartialCredit model using MML with the Swedish National Mathematics 1 dataset. ```python >>> from irtorch.models import GeneralizedPartialCredit >>> from irtorch.estimation_algorithms import MML >>> from irtorch.load_dataset import swedish_national_mathematics_1 >>> data = swedish_national_mathematics_1() >>> model = GeneralizedPartialCredit(data) >>> model.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### MonotonePolynomialModule Get Coefficients Source: https://irtorch.readthedocs.io/en/latest/torch_modules.html Retrieves the polynomial coefficients and intercept (if applicable) from the MonotonePolynomialModule. ```python def get_polynomial_coefficients(self) -> tuple[torch.Tensor, torch.Tensor]: """Returns the polynomial coefficients.""" pass ``` -------------------------------- ### Initialize Monotone Neural Network Model Source: https://irtorch.readthedocs.io/en/latest/notebooks/national_mathematics.html Initializes a Monotone Neural Network (MNN) model for comparison with the GPC model. Requires 'data_math' to be loaded. ```python irtorch.set_seed(42) mnn = MonotoneNN(data = data_math) ``` -------------------------------- ### item_theta_relationship_directions (NominalResponse) Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Gets the relationship directions between each item and latent variable for a fitted Nominal Response model. ```APIDOC ## item_theta_relationship_directions ### Description Gets the relationship directions between each item and latent variable for a fitted Nominal Response model. ### Method `item_theta_relationship_directions(_theta : Tensor = None_)` ### Parameters #### Path Parameters * **theta** (_torch.Tensor_ _,__optional_) – Only needed for NR models. A 2D tensor with latent theta scores from the population of interest. Each row represents one respondent, and each column represents a latent variable. ### Returns A 2D tensor with the relationships between the items and latent variables. Items are rows and latent variables are columns. ### Return type torch.Tensor ``` -------------------------------- ### Load and Preprocess IPIP-FFM Dataset Source: https://irtorch.readthedocs.io/en/latest/datasets.html Loads the IPIP-FFM dataset from a URL, performs extensive preprocessing including data cleaning, normalization, and reverse-coding, and converts it to a PyTorch tensor. This snippet is useful for preparing the dataset for analysis. ```python import os import urllib.request import shutil import tempfile import torch import pandas as pd data_url = "https://openpsychometrics.org/_rawdata/IPIP-FFM-data-8Nov2018.zip" with tempfile.TemporaryDirectory() as tmpdirname: zip_filepath = os.path.join(tmpdirname, "big_five.zip") data_dir = os.path.join(tmpdirname, "IPIP-FFM-data-8Nov2018") filepath = os.path.join(data_dir, "data-final.csv") urllib.request.urlretrieve(data_url, zip_filepath) shutil.unpack_archive(zip_filepath, tmpdirname) original_data = pd.read_csv(filepath, sep=" ", header=0) data = original_data.copy() data.iloc[:, 50:100] = data.iloc[:, 50:100] / 1000 # time in seconds # drop anyone with negative or really long response times (5min+). keep = ((data.iloc[:, 50:100] < 0).sum(axis=1) == 0) & ((data.iloc[:, 50:100] > 300).sum(axis=1) == 0) data = data[keep] data = data[data["IPC"] == 1] # Drop multiple submissions from same IP address. data = data.iloc[:, :50] # keep only response columns data = data.dropna() # Drop people with NaN values. data = data[data.sum(1) > 0] # Drop people with all missing responses (0 is missing). data = data.mask(data == 0, float("nan")) # encode missing as nan # Reverse-code reverse-coded items. reverse_items = [ "EXT2", "EXT4", "EXT6", "EXT8", "EXT10", "AGR1", "AGR3", "AGR5", "AGR7", "CSN2", "CSN4", "CSN6", "CSN8", "EST2", "EST4", "OPN2", "OPN4", "OPN6" ] data[reverse_items] = ((1 - data[reverse_items] / 5) * 5 + 1).mask(lambda col: col == 6, 0) data = data - 1 # code first option as 0, second as 1, etc. torch_data = torch.tensor(data.to_numpy(), dtype=torch.float32) ``` -------------------------------- ### Plot Model Training History Source: https://irtorch.readthedocs.io/en/latest/notebooks/national_mathematics.html Visualizes the training history of the fitted GPC and MNN models to assess convergence. The '.show()' method displays the plots. ```python gpc.plot.training_history().show() mnn.plot.training_history().show() ``` -------------------------------- ### bit_score_starting_theta_mc Source: https://irtorch.readthedocs.io/en/latest/scales.html Approximates the starting theta score for computing bit scores in multiple-choice models. It allows customization of the theta estimation method, device, learning rate, items used, and guessing probabilities. ```APIDOC ## bit_score_starting_theta_mc ### Description For multiple choice models, approximate the starting theta score θ(0) from which to compute bit scores. See notes under `bit_scores()` for the bit score formula. ### Parameters #### Optional Parameters - **theta_estimation** (str) - Method used to obtain the theta scores. Can be ‘NN’, ‘ML’, ‘EAP’ or ‘MAP’ for neural network, maximum likelihood, expected a posteriori or maximum a posteriori respectively. (default is ‘ML’) - **ml_map_device** (str) - For ML and MAP. The device to use for computation. Can be ‘cpu’ or ‘cuda’. (default is “cuda” if available else “cpu”) - **lbfgs_learning_rate** (float) - For ML and MAP. The learning rate to use for the L-BFGS optimizer. (default is 0.3) - **items** (list[int] | None) - The item indices for the items to use to compute the bit scores. (default is None and uses all items) - **guessing_probabilities** (list[float] | None) - The guessing probability for each item. The same length as the number of items. Guessing is not supported for polytomously scored items and the probabilities for them will be ignored. (default is None and uses no guessing or, for multiple choice models, 1 over the number of item categories) - **guessing_iterations** (int) - The number of iterations to use for approximating a minimum theta when guessing is incorporated. (default is 200) ### Returns A tensor with all the starting theta values. ### Return Type torch.Tensor ``` -------------------------------- ### Fit MonotoneNN Model Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Instantiate and fit the MonotoneNN model using MML estimation. Requires data and the MML algorithm. ```python >>> from irtorch.models import MonotoneNN >>> from irtorch.estimation_algorithms import MML >>> from irtorch.load_dataset import swedish_national_mathematics_1 >>> data = swedish_national_mathematics_1() >>> model = MonotoneNN(data) >>> model.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### IRT Model Item-Theta Relationship Directions Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Gets the relationship directions between items and latent variables for a fitted IRT model. For NR models, a theta tensor may be required to compute these directions. ```python item_theta_relationship_directions(_theta : Tensor = None_) → Tensor ``` -------------------------------- ### Initialize and Fit Nested Logit Models Source: https://irtorch.readthedocs.io/en/latest/notebooks/swedish_sat.html Creates deep copies of a pre-fitted 3PL model to serve as the correct response model for two new NestedLogit instances: one with a 'nominal' incorrect response model and another with a 'bspline' incorrect response model. Both models are then fitted to the training data using the MML algorithm and saved. ```python threepl_nominal = copy.deepcopy(threepl) threepl_bspline = copy.deepcopy(threepl) nested_nominal = NestedLogit(mc_correct = correct_responses, correct_response_model = threepl_nominal, incorrect_response_model = "nominal", data = train_data) nested_bspline = NestedLogit(mc_correct = correct_responses, correct_response_model = threepl_bspline, incorrect_response_model = "bspline", data = train_data) nested_nominal.fit(train_data=train_data, algorithm=MML()) nested_bspline.fit(train_data=train_data, algorithm=MML()) nested_nominal.save_model("swesat_nested_nominal.pt") nested_bspline.save_model("swesat_nested_bspline.pt") ``` -------------------------------- ### Fit ThreeParameterLogistic Model Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Demonstrates how to instantiate and fit a ThreeParameterLogistic model using the MML estimation algorithm with the Swedish SAT binary dataset. ```python >>> from irtorch.models import ThreeParameterLogistic >>> from irtorch.estimation_algorithms import MML >>> from irtorch.load_dataset import swedish_sat_binary >>> # Use quantitative part of the SAT data >>> data = swedish_sat_binary()[:, :80] >>> model = ThreeParameterLogistic(items=80) >>> model.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### Link Common Items Between IRT Models Source: https://irtorch.readthedocs.io/en/latest/scales.html Use `LinkCommonItems` to link the theta scales of two IRT models using common items. This example demonstrates fitting two models and then linking the scale of the second model to the first. ```python import irtorch from irtorch.rescale import LinkCommonItems from irtorch.models import ThreeParameterLogistic from irtorch.estimation_algorithms import JML, MML data = irtorch.load_dataset.swedish_sat_binary()[:, :80] # As an illustration, we split the dataset into two parts and use 20 common items. # In practice, we would of course use different datasets for each model. data1 = data[:2500, :50] data2 = data[2500:, 30:] model1 = ThreeParameterLogistic(items=50) model2 = ThreeParameterLogistic(items=50) model1.fit(train_data=data1, algorithm=MML()) model2.fit(train_data=data2, algorithm=JML()) # Link the scale of model 2 to the model 1 scale using common items. link = LinkCommonItems(model2, model1, list(range(20)), list(range(30, 50))) link.fit(theta_from = model2.latent_scores(data2), learning_rate=0.01, max_epochs=1000) model2.add_scale_transformation(link) # Plot the transformation model2.plot.scale_transformations(input_theta_range=(-5, 5)).show() ``` -------------------------------- ### Fit Mixed IRT Models Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Example of fitting a mix of Generalized Partial Credit (GPC) and Monotone Neural Network (MNN) models. This snippet demonstrates initializing different IRT models for specific item subsets and combining them into a single ModelMix for fitting. ```python from irtorch.models import GeneralizedPartialCredit, MonotoneNN, ModelMix from irtorch.estimation_algorithms import MML from irtorch.load_dataset import swedish_national_mathematics_2 data_math = swedish_national_mathematics_2() gpc = GeneralizedPartialCredit(data=data_math[:, :14]) mnn = MonotoneNN(data = data_math[:, 14:]) mix_model = ModelMix([gpc, mnn]) mix_model.fit(train_data=data_math, algorithm=MML()) ``` -------------------------------- ### MonotonePolynomialModule Initialization Source: https://irtorch.readthedocs.io/en/latest/torch_modules.html Initializes a MonotonePolynomialModule with specified degree, input/output features, and optional relationship matrix. Ensure the degree is an odd number. ```python MonotonePolynomialModule(degree=3, in_features=2, out_features=1, relationship_matrix=torch.tensor([[1.0, 0.0], [0.0, 1.0]])) ``` -------------------------------- ### Torch Module Parameters Source: https://irtorch.readthedocs.io/en/latest/torch_modules.html This section outlines the parameters available for configuring Torch modules, including details on their types and default values. ```APIDOC ## Torch Module Parameters ### Description Details the configurable parameters for Torch modules. ### Parameters - **variables** (int) - The number of variables (dimensions) to transform. - **num_bins** (int, optional) - The number of bins to use for the spline (default is 30). - **lower_input_bound** (float, optional) - The left boundary of the transformation interval (default is 0.0). - **upper_input_bound** (float, optional) - The right boundary of the transformation interval (default is 1.0). - **lower_output_bound** (float, optional) - The bottom boundary of the transformation interval (default is 0.0). - **upper_output_bound** (float, optional) - The top boundary of the transformation interval (default is 1.0). - **min_bin_width** (float, optional) - The minimum width of each bin (default is 1e-3). - **min_bin_height** (float, optional) - The minimum height of each bin (default is 1e-3). - **min_derivative** (float, optional) - The minimum derivative value at the knots (default is 1e-3). - **free_endpoints** (bool, optional) - When free_endpoints=True, the lower and upper output bounds become trainable parameters (default is False). ``` -------------------------------- ### irtorch.load_dataset.swedish_sat_verbal Source: https://irtorch.readthedocs.io/en/latest/datasets.html Load a sample from the verbal part of the Swedish SAT dataset and the correct item responses. The correct item responses start from one, and thus a 1 corresponds to a response of 0 in the data. Returns a tuple containing the loaded dataset and the correct item responses. ```APIDOC ## irtorch.load_dataset.swedish_sat_verbal ### Description Load a sample from the verbal part of the Swedish SAT dataset and the correct item responses. The correct item responses start from one, and thus a 1 corresponds to a response of 0 in the data. ### Returns A tuple containing the loaded dataset and the correct item responses. ### Return type tuple (torch.Tensor, list[int]) ``` -------------------------------- ### Fit MonotonePolynomial Model Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Instantiate and fit the MonotonePolynomial model using MML estimation. Requires data and the MML algorithm. ```python >>> from irtorch.models import MonotonePolynomial >>> from irtorch.estimation_algorithms import MML >>> from irtorch.load_dataset import swedish_national_mathematics_1 >>> data = swedish_national_mathematics_1() >>> model = MonotonePolynomial(data) >>> model.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### Fit GPC and MNN Models with Data Splitting Source: https://irtorch.readthedocs.io/en/latest/notebooks/national_mathematics.html Fits the GPC and MNN models using MML and JML algorithms respectively, after splitting the data into training and testing sets. The fitting process utilizes GPU if available, otherwise CPU. Specify 'device' argument for explicit control. ```python irtorch.set_seed(42) train_data, test_data = irtorch.split_data(data_math, 0.8) gpc.fit(train_data=train_data, algorithm=MML()) mnn.fit(train_data=train_data, algorithm=JML()) ``` -------------------------------- ### irtorch.load_dataset.swedish_sat Source: https://irtorch.readthedocs.io/en/latest/datasets.html Load a sample from Swedish SAT dataset and the correct item responses. The correct item responses start from one, and thus a 1 corresponds to a response of 0 in the data. The first 80 items are from the quantitative test, and the last 80 items are from the verbal test. Returns a tuple containing the loaded dataset and the correct item responses. ```APIDOC ## irtorch.load_dataset.swedish_sat ### Description Load a sample from Swedish SAT dataset and the correct item responses. The correct item responses start from one, and thus a 1 corresponds to a response of 0 in the data. The first 80 items are from the quantitative test, and the last 80 items are from the verbal test. ### Returns A tuple containing the loaded dataset and the correct item responses. ### Return type tuple (torch.Tensor, list[int]) ``` -------------------------------- ### Initialize and Fit Flow Transformation Source: https://irtorch.readthedocs.io/en/latest/scales.html Demonstrates how to initialize a Flow object, fit it to existing theta scores, and add it to an IRT model. This is typically used to rescale theta scores to a new distribution. ```python import irtorch from irtorch.models import GradedResponse from irtorch.estimation_algorithms import MML from irtorch.rescale import Flow data = irtorch.load_dataset.swedish_national_mathematics_1() model = GradedResponse(data) model.fit(train_data=data, algorithm=MML()) thetas = model.latent_scores(data) # Initalize and fit the flow scale transformation. Supply it to the model. flow = Flow(1) flow.fit(thetas) model.add_scale_transformation(flow) # Estimate thetas on the transformed scale rescaled_thetas = model.latent_scores(data) # Or alternatively by directly converting the old ones rescaled_thetas = model.transform_theta(thetas) # Plot the differences model.plot.latent_score_distribution(thetas).show() model.plot.latent_score_distribution(rescaled_thetas).show() # Put the thetas back to the original scale original_thetas = model.inverse_transform_theta(rescaled_thetas) # Plot an item on the flow transformed scale model.plot.item_probabilities(1).show() ``` -------------------------------- ### Prepare Binary Training Data Source: https://irtorch.readthedocs.io/en/latest/notebooks/swedish_sat.html Converts training and testing data into a binary format (correct/incorrect responses) suitable for the 3PL model. Ensure the data is a PyTorch tensor. ```python train_data_binary = (train_data == torch.tensor(correct_responses)).float() test_data_binary = (test_data == torch.tensor(correct_responses)).float() ``` -------------------------------- ### Fit 2PL Model with MML Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Shows how to fit a Two-Parameter Logistic model using MML estimation with the Swedish SAT binary dataset. ```python >>> from irtorch.models import TwoParameterLogistic >>> from irtorch.estimation_algorithms import MML >>> from irtorch.load_dataset import swedish_sat_binary >>> # Use quantitative part of the SAT data >>> data = swedish_sat_binary()[:, :80] >>> model = TwoParameterLogistic(items=80) >>> model.fit(train_data=data, algorithm=MML()) ``` -------------------------------- ### irtorch.load_dataset.swedish_national_mathematics_1() Source: https://irtorch.readthedocs.io/en/latest/datasets.html Loads the first Swedish National Mathematics dataset. ```APIDOC ## irtorch.load_dataset.swedish_national_mathematics_1() ### Description Loads the first Swedish National Mathematics dataset. ### Method `irtorch.load_dataset.swedish_national_mathematics_1()` ### Returns - A preprocessed version of the Swedish National Mathematics dataset 1. - The original dataset as a pandas DataFrame. - The items in the dataset as a list of strings. ``` -------------------------------- ### NeuralSplineFlow Initialization Source: https://irtorch.readthedocs.io/en/latest/torch_modules.html Initializes a NeuralSplineFlow with a transformation and a base distribution. This module is used for normalizing flows. ```python NeuralSplineFlow(transformation=RationalQuadraticSpline(variables=10), distribution=torch.distributions.Normal(loc=0.0, scale=1.0)) ``` -------------------------------- ### irtorch.load_dataset.swedish_sat_binary() Source: https://irtorch.readthedocs.io/en/latest/datasets.html Loads the binary version of the Swedish SAT dataset. ```APIDOC ## irtorch.load_dataset.swedish_sat_binary() ### Description Loads the binary version of the Swedish SAT dataset. ### Method `irtorch.load_dataset.swedish_sat_binary()` ### Returns - A preprocessed binary version of the Swedish SAT dataset. - The original dataset as a pandas DataFrame. - The items in the dataset as a list of strings. ``` -------------------------------- ### Load Swedish SAT Quantitative Part with Correct Responses Source: https://irtorch.readthedocs.io/en/latest/datasets.html Loads a sample from the quantitative part of the Swedish SAT dataset and the correct item responses. Returns a tuple of a PyTorch tensor and a list of integers. ```python irtorch.load_dataset.swedish_sat_quantitative() ``` -------------------------------- ### Evaluate Model Performance Source: https://irtorch.readthedocs.io/en/latest/notebooks/swedish_sat.html Compares log-likelihood and accuracy between nominal and bspline models using test data. Also prints mean metrics for each model. ```python print(f"nominal log-likelihood: {round(nested_nominal.evaluate.log_likelihood(data=test_data, theta=theta_test_nominal).item())}") print(f"bspline log-likelihood: {round(nested_bspline.evaluate.log_likelihood(data=test_data, theta=theta_test_bspline).item())}") print(f"nominal accuracy: {round(nested_nominal.evaluate.accuracy(data=test_data, theta=theta_test_nominal).item(), 3)}") print(f"bspline accuracy: {round(nested_bspline.evaluate.accuracy(data=test_data, theta=theta_test_bspline).item(), 3)}") print(f"nominal metrics:\n{round(nested_nominal.evaluate.predictions(data=test_data, theta=theta_test_nominal).mean(axis=0), 3)} --------------------") print(f"bspline metrics:\n{round(nested_bspline.evaluate.predictions(data=test_data, theta=theta_test_bspline).mean(axis=0), 3)} --------------------") ``` -------------------------------- ### Print Infit and Outfit Statistics Source: https://irtorch.readthedocs.io/en/latest/notebooks/swedish_sat.html Prints the computed infit and outfit statistics for both the nominal (NR) and bspline (MMCNN) models. ```python print(f"NR infit: {infit_item_nr}") print(f"MMCNN infit: {infit_item_mmcnn}") print(f"NR outfit: {outfit_item_nr}") print(f"MMCNN outfit: {outfit_item_mmcnn}") ``` -------------------------------- ### irtorch.load_dataset.swedish_sat_quantitative() Source: https://irtorch.readthedocs.io/en/latest/datasets.html Loads the quantitative version of the Swedish SAT dataset. ```APIDOC ## irtorch.load_dataset.swedish_sat_quantitative() ### Description Loads the quantitative version of the Swedish SAT dataset. ### Method `irtorch.load_dataset.swedish_sat_quantitative()` ### Returns - A preprocessed quantitative version of the Swedish SAT dataset. - The original dataset as a pandas DataFrame. - The items in the dataset as a list of strings. ``` -------------------------------- ### irtorch.load_dataset.swedish_national_mathematics_2() Source: https://irtorch.readthedocs.io/en/latest/datasets.html Loads the second Swedish National Mathematics dataset. ```APIDOC ## irtorch.load_dataset.swedish_national_mathematics_2() ### Description Loads the second Swedish National Mathematics dataset. ### Method `irtorch.load_dataset.swedish_national_mathematics_2()` ### Returns - A preprocessed version of the Swedish National Mathematics dataset 2. - The original dataset as a pandas DataFrame. - The items in the dataset as a list of strings. ``` -------------------------------- ### irtorch.load_dataset.big_five() Source: https://irtorch.readthedocs.io/en/latest/datasets.html Download and preprocess the Big Five personality dataset. This dataset assesses personality traits using the Five-Factor Model (OCEAN). It includes responses to 50 items on a five-point scale, along with demographic and contextual information. ```APIDOC ## irtorch.load_dataset.big_five() ### Description Download and preprocess the Big Five personality dataset. This dataset assesses personality traits using the Five-Factor Model (OCEAN). It includes responses to 50 items on a five-point scale, along with demographic and contextual information. ### Method `irtorch.load_dataset.big_five()` ### Returns - A preprocessed version of the big-five dataset. - The original dataset as a pandas DataFrame. - The items in the dataset as a list of strings. ``` -------------------------------- ### Split Data into Training and Testing Sets Source: https://irtorch.readthedocs.io/en/latest/notebooks/swedish_sat.html Splits the loaded dataset into training (80%) and testing (20%) sets using a specified seed for reproducibility. This is crucial for model evaluation and preventing overfitting. ```python irtorch.set_seed(42) train_data, test_data = irtorch.split_data(data_sat, 0.8) ``` -------------------------------- ### Model Convergence and Q3 Analysis Source: https://irtorch.readthedocs.io/en/latest/notebooks/national_mathematics.html This snippet shows the output indicating model convergence and the largest Q3 value, which suggests potential item dependency. This output is typically generated after running a model fitting process. ```text Iteration 4: Current Loss = 23594.384765625 INFO: Converged at iteration 4. INFO: Largest Q3 is 0.60 between Item 21 and Item 22. Computing Q3 p-values by null distribution sampling. Sample: 1000 gpc: Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Item 7 Item 1 NaN 0.314 0.484 0.074 0.086 0.900 0.700 Item 2 0.314 NaN 0.238 0.674 0.360 0.878 0.024 Item 3 0.484 0.238 NaN 0.120 0.924 0.138 0.978 Item 4 0.074 0.674 0.120 NaN 0.338 0.574 0.752 Item 5 0.086 0.360 0.924 0.338 NaN 0.998 0.278 Item 6 0.900 0.878 0.138 0.574 0.998 NaN 0.438 Item 7 0.700 0.024 0.978 0.752 0.278 0.438 NaN mnn: Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Item 7 Item 1 NaN 0.638 0.222 0.180 0.194 0.438 0.808 Item 2 0.638 NaN 0.040 0.358 0.994 0.642 0.052 Item 3 0.222 0.040 NaN 0.578 0.400 0.444 0.552 Item 4 0.180 0.358 0.578 NaN 0.942 0.842 0.820 Item 5 0.194 0.994 0.400 0.942 NaN 0.952 0.274 Item 6 0.438 0.642 0.444 0.842 0.952 NaN 0.612 Item 7 0.808 0.052 0.552 0.820 0.274 0.612 NaN ``` -------------------------------- ### Load Swedish SAT Dataset with Correct Responses Source: https://irtorch.readthedocs.io/en/latest/datasets.html Loads a sample from the Swedish SAT dataset and the correct item responses. The correct responses are 1-indexed. Returns a tuple of a PyTorch tensor and a list of integers. ```python irtorch.load_dataset.swedish_sat() ``` -------------------------------- ### Fit 1PL Model with MML Source: https://irtorch.readthedocs.io/en/latest/irt_models.html Demonstrates fitting a One-Parameter Logistic model using the MML estimation algorithm with the Swedish SAT binary dataset. ```python >>> from irtorch.models import OneParameterLogistic >>> from irtorch.estimation_algorithms import MML >>> from irtorch.load_dataset import swedish_sat_binary >>> # Use quantitative part of the SAT data >>> data = swedish_sat_binary()[:, :80] >>> model = OneParameterLogistic(items=80) >>> model.fit(train_data=data, algorithm=MML()) ```