diff --git a/app.qmd b/app.qmd new file mode 100644 index 0000000..e4a8bee --- /dev/null +++ b/app.qmd @@ -0,0 +1,77 @@ +--- +title: "Data Science Methods for Forecasting in Energy and Economics" +date: 2025-07-10 +format: + revealjs: + embed-resources: true + footer: "" +execute: + daemon: false +highlight-style: github +--- + +#### B-spline Basis Functions + +```{ojs} +bsplineData = FileAttachment("basis_functions.csv").csv({ typed: true }) +``` + +```{ojs} +knotValues = Array.from(new Set(bsplineData.map(d => d.knots))).sort((a, b) => a - b) +minKnots = Math.min(...knotValues) +maxKnots = Math.max(...knotValues) + +muValues = Array.from(new Set(bsplineData.map(d => d.mu))).sort((a, b) => a - b) +minMu = Math.min(...muValues) +maxMu = Math.max(...muValues) +``` + +```{ojs} +// Create a more compact layout for controls +viewof controls = Inputs.form({ + knots: Inputs.range([minKnots, maxKnots], {value: minKnots, step: 1, label: "Knots:", width: 200}), + mu: Inputs.range([minMu, maxMu], {value: 0.5, step: 0.1, label: "μ:", width: 200}) +}, { + submit: false, + layout: 'horizontal', + style: 'display: flex; gap: 20px; align-items: center; margin-bottom: 10px; font-size: 0.9em;' +}) + +selectedKnots = controls.knots +selectedMu = controls.mu +``` + +```{ojs} +filteredBspline = bsplineData.filter(function(row) { + return selectedKnots == row.knots && Math.abs(selectedMu - row.mu) < 0.001; +}) +``` + +```{ojs} +Plot.plot({ + grid: true, + y: {domain: [0, 0.7]}, + x: {label: "x", domain: [0, 1]}, + marks: [ + Plot.line(filteredBspline, { + x: "x", + y: "y", + stroke: "b", + strokeWidth: 5, + }), + Plot.ruleY([0]) + ], + color: { + legend: false, + label: "Basis Function" + }, + marginRight: 80, + width: 800, + height: 400, + // title: `B-spline Basis Functions (${selectedKnots} knots, μ = ${selectedMu})` +}) +``` + +::: {.callout-note} +TODO +::: diff --git a/test.R b/test.R new file mode 100644 index 0000000..56f6781 --- /dev/null +++ b/test.R @@ -0,0 +1,52 @@ +# %% +library(profoc) +library(ggplot2) +library(tidyr) +library(dplyr) +library(readr) + +# Creating faceted plots for different knot values and mu values +# Create a function to generate the data for a given number of knots and mu value +generate_basis_data <- function(num_knots, mu_value) { + B <- profoc:::make_basis_matrix(1:99 / 100, + profoc::make_knots( + n = num_knots, + mu = mu_value, + sig = 1, + nonc = 0, + deg = 1 + ), + deg = 3 + ) + df <- as.data.frame(as.matrix(B)) + df$x <- 1:99 / 100 + df_long <- pivot_longer(df, + cols = -x, + names_to = "b", + values_to = "y" + ) + df_long$knots <- num_knots # Store knot count as numeric + df_long$mu <- mu_value # Add mu parameter information + return(df_long) +} + +# Generate data for each combination of knot value and mu value +knot_values <- 5:10 +mu_values <- seq(0.1, 0.9, by = 0.1) + +# Create an empty list to store all combinations +all_data <- list() +counter <- 1 + +# Generate data for all combinations +for (k in knot_values) { + for (m in mu_values) { + all_data[[counter]] <- generate_basis_data(k, m) + counter <- counter + 1 + } +} + +# Combine all data frames +all_data <- bind_rows(all_data) + +write_csv(all_data, "basis_functions.csv")