| Title: | Bayesian Knowledge Tracing Model |
|---|---|
| Description: | Fitting, cross-validating, and predicting with Bayesian Knowledge Tracing (BKT) models. It is designed for analyzing educational datasets to trace student knowledge over time. The package includes functions for fitting BKT models, evaluating their performance using various metrics, and making predictions on new data. It provides functionality similar to the 'Python' package 'pyBKT' authored by Zachary A. Pardos (zp@berkeley.edu) at <https://github.com/CAHLR/pyBKT>. |
| Authors: | Yuhao Yuan [aut, cre], Biying Zhou [aut], Feng Ji [aut] |
| Maintainer: | Yuhao Yuan <yuanyuhaoapply@163.com> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-10-05 12:53:46 UTC |
| Source: | https://github.com/cran/BKT |
Create a BKT (Bayesian Knowledge Tracing) model object with initial parameters. This function constructs a BKT model by taking in various parameters such as parallelization options, number of fits, random seed, and other model-specific settings. These parameters can later be modified during the fitting or cross-validation process.
bkt( parallel = TRUE, num_fits = 5, folds = 5, seed = sample(1:1e+08, 1), model_type = rep(FALSE, 4), forgets = FALSE, fixed = NULL, defaults = NULL, ... )bkt( parallel = TRUE, num_fits = 5, folds = 5, seed = sample(1:1e+08, 1), model_type = rep(FALSE, 4), forgets = FALSE, fixed = NULL, defaults = NULL, ... )
parallel |
Logical. Indicates whether to use parallel computation.
If set to |
num_fits |
Integer. Number of fit iterations. The best model is selected from the total iterations. |
folds |
Integer. Number of folds used for cross-validation. This parameter is used during cross-validation to divide the data into parts. |
seed |
Numeric. Seed for the random number generator, which ensures reproducibility of results. |
model_type |
Logical vector. Specifies model variants to use. There are four possible variants: 'multilearn', 'multiprior', 'multipair', and 'multigs'. Each corresponds to a different modeling strategy. |
forgets |
Logical. Whether to include a forgetting factor in the model.
If set to |
fixed |
List. A nested list specifying which parameters to fix for specific skills during
model fitting. Each skill can have certain parameters, such as "guesses" and "slips", set to
|
defaults |
List. The defaults parameter is a list that functions as a query dictionary. It is used to map column names in the data to the expected variables in the model. This helps ensure that the model can work with different datasets that may have varying column names. |
... |
Other parameters. |
A BKT model object, which can be used by other functions such as fitting the model, cross-validation, or making predictions.
model <- bkt(seed = 42, parallel = FALSE, num_fits = 1)model <- bkt(seed = 42, parallel = FALSE, num_fits = 1)
Perform cross-validation on a BKT (Bayesian Knowledge Tracing) model. This function trains and evaluates the BKT model using cross-validation. It splits the dataset into training and validation sets, trains the model on the training data, and evaluates it on the validation data according to a specified metric.
crossvalidate( object, data = NULL, data_path = NULL, metric = rmse, parallel = FALSE, seed = NULL, num_fits = 1, folds = 5, forgets = FALSE, fixed = NULL, model_type = NULL, ... )crossvalidate( object, data = NULL, data_path = NULL, metric = rmse, parallel = FALSE, seed = NULL, num_fits = 1, folds = 5, forgets = FALSE, fixed = NULL, model_type = NULL, ... )
object |
A BKT model object. The model to be cross-validated. |
data |
Data frame. The dataset to be used for cross-validation. If |
data_path |
Character. The file path to the dataset. This will be used if |
metric |
Function. The metric function used to evaluate model performance. |
parallel |
Logical. Indicates whether to use parallel computation.
If set to |
seed |
Numeric. Seed for the random number generator, which ensures reproducibility of results. |
num_fits |
Integer. Number of fit iterations. The best model is selected from the total iterations. |
folds |
Integer. Number of folds used for cross-validation. This parameter is used during cross-validation to divide the data into parts. |
forgets |
Logical. Whether to include a forgetting factor in the model.
If set to |
fixed |
List. A nested list specifying which parameters to fix for specific skills during
model fitting. Each skill can have certain parameters, such as "guesses" and "slips", set to
|
model_type |
Logical vector. Specifies model variants to use. There are four possible variants: 'multilearn', 'multiprior', 'multipair', and 'multigs'. Each corresponds to a different modeling strategy. |
... |
Other parameters. |
A list containing the cross-validation results, including the average performance metric and any other relevant details from the validation process.
data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) cv_results <- crossvalidate(model, data = simulation_data_50, folds = 2) print(cv_results)data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) cv_results <- crossvalidate(model, data = simulation_data_50, folds = 2) print(cv_results)
Evaluate a BKT (Bayesian Knowledge Tracing) model using a specified metric. This function evaluates a fitted BKT model on a given dataset using a chosen performance metric. It takes either a data frame or a file path to the data and returns the evaluation result based on the specified metric (e.g., RMSE or accuracy).
evaluate(object, data = NULL, data_path = NULL, metric = rmse)evaluate(object, data = NULL, data_path = NULL, metric = rmse)
object |
A fitted BKT model object. This is the model to be evaluated. |
data |
Data frame. The dataset on which the model will be evaluated. If |
data_path |
Character. The file path to the dataset for evaluation. This will be used if |
metric |
Function or Function List. The evaluation metric used to assess the model performance. (Root Mean Square Error), but other metrics can also be specified. |
Numeric or List. The result of the evaluation based on the specified metric(s). For example, if rmse is used,
the function will return the root mean square error for the model on the dataset.
data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) result <- fit(model, data = simulation_data_50) eval_result <- evaluate(result, data = simulation_data_50) print(eval_result)data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) result <- fit(model, data = simulation_data_50) eval_result <- evaluate(result, data = simulation_data_50) print(eval_result)
Fetch a dataset from an online source. This function downloads a dataset from a provided URL and saves it to a specified location on the local system. The dataset must be publicly accessible, without requiring any password or authentication. It can then be used for further analysis or modeling.
fetch_dataset(object, link, loc)fetch_dataset(object, link, loc)
object |
A BKT model object. The model can use the fetched dataset for fitting or other tasks. |
link |
Character. The URL where the dataset is located. This must be a publicly accessible URL. |
loc |
Character. The local file path where the dataset will be saved. The dataset will be stored at this location after download. |
None. The function downloads the data file to the specified location.
## Not run: model <- bkt() fetch_dataset(model, "https://example.com/dataset.csv", tempdir()) ## End(Not run)## Not run: model <- bkt() fetch_dataset(model, "https://example.com/dataset.csv", tempdir()) ## End(Not run)
Fit a BKT (Bayesian Knowledge Tracing) model.
This function fits the BKT model using the provided data and various options, such as
skill filtering, forget model, and parallelization. The function uses the model object
created by bkt() and fits the data according to the specified parameters.
fit( object, data_path = NULL, data = NULL, parallel = FALSE, seed = NULL, num_fits = 1, forgets = FALSE, fixed = NULL, model_type = NULL, ... )fit( object, data_path = NULL, data = NULL, parallel = FALSE, seed = NULL, num_fits = 1, forgets = FALSE, fixed = NULL, model_type = NULL, ... )
object |
A BKT model object. The model to be cross-validated. |
data_path |
Character. The file path to the dataset. This will be used if |
data |
Data frame. The dataset to be used for cross-validation. If |
parallel |
Logical. Indicates whether to use parallel computation.
If set to |
seed |
Numeric. Seed for the random number generator, which ensures reproducibility of results. |
num_fits |
Integer. Number of fit iterations. The best model is selected from the total iterations. |
forgets |
Logical. Whether to include a forgetting factor in the model.
If set to |
fixed |
List. A nested list specifying which parameters to fix for specific skills during
model fitting. Each skill can have certain parameters, such as "guesses" and "slips", set to
|
model_type |
Logical vector. Specifies model variants to use. There are four possible variants: 'multilearn', 'multiprior', 'multipair', and 'multigs'. Each corresponds to a different modeling strategy. |
... |
Other parameters. |
A fitted BKT model object, which can be used for predictions, cross-validation, or parameter analysis.
data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) result <- fit( model, data = simulation_data_50 )data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) result <- fit( model, data = simulation_data_50 )
Load a BKT model from a file. This function loads a previously saved BKT model from an RDS file. The model attributes are restored into the provided model object, allowing it to be used for further analysis or predictions.
load_model(model, loc)load_model(model, loc)
model |
A BKT model object into which the saved model's attributes will be loaded. |
loc |
Character. The file path from which the model will be loaded, typically an |
The updated BKT model object with the restored attributes from the saved model.
data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) fitted_model <- fit(model, data = simulation_data_50) model_file <- tempfile(fileext = ".rds") save_model(fitted_model, model_file) loaded_model <- load_model(bkt(), model_file) unlink(model_file)data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) fitted_model <- fit(model, data = simulation_data_50) model_file <- tempfile(fileext = ".rds") save_model(fitted_model, model_file) loaded_model <- load_model(bkt(), model_file) unlink(model_file)
Extract fitted parameters from a BKT model. This function retrieves the parameters from a fitted BKT model object. The parameters include model-specific values such as "learns", "guesses", "slips", and "forgets". These parameters are returned in a format that is easy to print or manipulate for further analysis.
params(object)params(object)
object |
A fitted BKT model object. The model should have been previously fitted using
the |
A data frame containing the fitted model parameters. The data frame will typically include columns such as 'learns', 'guesses', 'slips', and other model-specific values.
data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) result <- fit(model, data = simulation_data_50) params_df <- params(result) print(params_df)data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) result <- fit(model, data = simulation_data_50) params_df <- params(result) print(params_df)
Predict outcomes using a fitted BKT model.
This function uses a trained Bayesian Knowledge Tracing (BKT) model to make predictions
on new data. The predictions include both the likelihood of a correct response (correct_predictions)
and the estimated hidden state of the learner's knowledge (state_predictions).
predict_bkt(model, data_path = NULL, data = NULL)predict_bkt(model, data_path = NULL, data = NULL)
model |
A trained BKT model object. The model must have been previously fitted using
the |
data_path |
Character. The file path to the dataset on which predictions will be made. If this is provided, the function will read data from the file. |
data |
Data frame. A pre-loaded dataset to be used for predictions. This can be used
instead of specifying |
A data frame containing the original data with two additional columns:
correct_predictions and state_predictions.
data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) fit_model <- fit(model, data = simulation_data_50) predictions <- predict_bkt(fit_model, data = simulation_data_50) head(predictions)data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) fit_model <- fit(model, data = simulation_data_50) predictions <- predict_bkt(fit_model, data = simulation_data_50) head(predictions)
Save a BKT model to a file.
This function saves a trained BKT model to a specified file location. The model is stored
as an RDS file, which can be loaded back into R using the load_model() function.
save_model(model, loc)save_model(model, loc)
model |
A trained BKT model object to be saved. |
loc |
Character. The file path where the model will be saved, typically with an |
None. The function saves the model to the specified location.
data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) fit_model <- fit(model, data = simulation_data_50) model_file <- tempfile(fileext = ".rds") save_model(fit_model, model_file) unlink(model_file)data("simulation_data_50", package = "BKT") model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) fit_model <- fit(model, data = simulation_data_50) model_file <- tempfile(fileext = ".rds") save_model(fit_model, model_file) unlink(model_file)
This function sets or initializes the parameters of a Bayesian Knowledge Tracing (BKT) model. The user can manually specify the values for different parameters associated with specific skills.
set_coef(object, values)set_coef(object, values)
object |
An object of the BKT model. This is the model for which the parameters will be set or initialized. |
values |
A list containing the skill names and their corresponding BKT parameters.
Each skill should have its own list of parameters.
The parameters can include 'prior', 'learns', 'forgets', 'guesses', and 'slips'.
Example structure: |
This function allows users to manually specify or update the parameters of a BKT model for different skills. The values should be provided as a named list, with each skill having its own sublist of BKT parameters. The function performs checks to ensure that the provided parameters are valid in terms of type, length, and existence.
The updated BKT model object with the newly set coefficients.
data("simulation_data_50", package = "BKT") # Initialize a BKT model model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) # Set custom parameters for a specific skill model <- set_coef(model, list( "mathematic" = list("prior" = 0.5, "learns" = 0.2) )) # Fit the model with fixed parameters result <- fit(model, forgets = TRUE, data = simulation_data_50, skills = "mathematic", fixed = list("mathematic" = list("prior" = TRUE)) )data("simulation_data_50", package = "BKT") # Initialize a BKT model model <- bkt(seed = 42, parallel = FALSE, num_fits = 1) # Set custom parameters for a specific skill model <- set_coef(model, list( "mathematic" = list("prior" = 0.5, "learns" = 0.2) )) # Fit the model with fixed parameters result <- fit(model, forgets = TRUE, data = simulation_data_50, skills = "mathematic", fixed = list("mathematic" = list("prior" = TRUE)) )
This function generates simulated student response data based on the BKT model. Each student is modeled as having a latent skill mastery state that can evolve over a sequence of activities according to specified learning, guessing, and slipping probabilities. The generated dataset can be used for testing, model evaluation, or demonstration purposes.
simulate_bkt_data( prior, guess, slip, learn, num_students, min_questions, max_questions, output_file = NULL )simulate_bkt_data( prior, guess, slip, learn, num_students, min_questions, max_questions, output_file = NULL )
prior |
Numeric. Initial probability that a student has mastered the skill before any questions. Corresponds to the BKT parameter \(P_p\). |
guess |
Numeric. Probability that a student answers correctly despite not mastering the skill. Corresponds to the BKT parameter \(P_g\). |
slip |
Numeric. Probability that a student answers incorrectly despite mastering the skill. Corresponds to the BKT parameter \(P_s\). |
learn |
Numeric. Probability that a student transitions from the unmastered to mastered state after each activity. Corresponds to the BKT parameter \(P_l\). |
num_students |
Integer. Number of students to simulate. |
min_questions |
Integer. Minimum number of questions (activities) per student. |
max_questions |
Integer. Maximum number of questions (activities) per student.
Each student's actual sequence length is randomly drawn from 1 to |
output_file |
Character. Optional file path. If provided, the generated dataset will be written to a CSV file. |
The simulated responses follow the standard BKT transition logic:
and the observed correctness is drawn as:
A data frame with the following columns:
order_id: Integer, the order of the question within a student's sequence.
correct: Binary (0/1), indicating whether the student answered correctly.
student_id: Integer, the unique identifier for each student.
skill_name: Character, the skill associated with the responses (currently fixed as "mathematic").
prior <- 0.2 guess <- 0.1 slip <- 0.1 learn <- 0.3 num_students <- 5 min_questions <- 5 max_questions <- 10 simulated_data <- simulate_bkt_data( prior = prior, guess = guess, slip = slip, learn = learn, num_students = num_students, min_questions = min_questions, max_questions = max_questions ) head(simulated_data)prior <- 0.2 guess <- 0.1 slip <- 0.1 learn <- 0.3 num_students <- 5 min_questions <- 5 max_questions <- 10 simulated_data <- simulate_bkt_data( prior = prior, guess = guess, slip = slip, learn = learn, num_students = num_students, min_questions = min_questions, max_questions = max_questions ) head(simulated_data)
Simulated dataset with 2000 students
simulation_data_2000simulation_data_2000
An object of class data.frame with 19987 rows and 4 columns.
A simulated BKT dataset containing 50 students with maximum sequence length of 10.
simulation_data_50simulation_data_50
A data frame with 50 students:
Student ID
Skill name
response ID
Binary outcome (0/1)
Simulated using RBKT simulation functions.
Simulated dataset with 500 students
simulation_data_500simulation_data_500
An object of class data.frame with 5020 rows and 4 columns.