Skip to content

Commit 58bfa30

Browse files
authored
Merge pull request #10 from tbeason/timevarying
add varying coef models
2 parents 21f7c3a + f2309eb commit 58bfa30

File tree

7 files changed

+852
-5
lines changed

7 files changed

+852
-5
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ jobs:
2525
arch:
2626
- x64
2727
steps:
28-
- uses: actions/checkout@v2
28+
- uses: actions/checkout@v3
2929
- uses: julia-actions/setup-julia@v1
3030
with:
3131
version: ${{ matrix.version }}
3232
arch: ${{ matrix.arch }}
33-
- uses: actions/cache@v1
33+
- uses: actions/cache@v4
3434
env:
3535
cache-name: cache-artifacts
3636
with:

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NonparametricRegression"
22
uuid = "db432338-e110-4b7a-9c53-0ace38eb8f7f"
33
authors = ["Tyler Beason <tbeas12@gmail.com>"]
4-
version = "0.2.1"
4+
version = "0.2.2"
55

66
[deps]
77
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
@@ -11,7 +11,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1111

1212
[compat]
1313
DocStringExtensions = "0.8, 0.9"
14-
StaticArrays = "1.2, 1.3, 1.4, 1.5"
14+
StaticArrays = "1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9"
1515
julia = "1.6"
1616

1717
[extras]

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,48 @@ The two important exported convenience methods are `npregress` and `optimalbandw
2424

2525
## Examples
2626

27+
### Basic univariate regression
2728
```julia
2829
using NonparametricRegression
2930

30-
npregress
31+
# Generate some data
32+
x = rand(100)
33+
y = sin.(2π * x) + 0.1 * randn(100)
3134

35+
# Estimate with automatic bandwidth selection
36+
ŷ = npregress(x, y; method=:ll, bandwidthselection=:aicc)
37+
38+
# Or specify bandwidth manually
39+
ŷ_manual = npregress(x, y, x, 0.1; method=:lc)
40+
```
41+
42+
### Varying coefficient models
43+
```julia
44+
using NonparametricRegression
45+
46+
# Generate data with state-dependent coefficients
47+
N = 500
48+
z = rand(N) # State variable
49+
X = [ones(N) randn(N, 2)] # Design matrix: intercept + 2 covariates
50+
51+
# True varying coefficients
52+
β₁(z) = 1 + z
53+
β₂(z) = 2 - z^2
54+
β₃(z) = 0.5 * sin(2π * z)
55+
56+
# Generate response
57+
y = [X[i, :]' * [β₁(z[i]), β₂(z[i]), β₃(z[i])] for i in 1:N] + 0.1 * randn(N)
58+
59+
# Estimate varying coefficients
60+
zgrid = 0:0.1:1
61+
B = npvaryingcoef(X, y, z, zgrid; method=:ll, bandwidthselection=:aicc)
62+
63+
# B is a 3×11 matrix where B[:, i] contains the coefficients at zgrid[i]
64+
65+
# Predict for new data
66+
X_new = [ones(10) randn(10, 2)]
67+
z_new = rand(10)
68+
y_pred = predict_varyingcoef(X_new, z_new, B, zgrid, 0.1; method=:ll)
3269
```
3370

3471
## Detail

src/NonparametricRegression.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ export localconstant, localconstantweights
1616
export locallinear, llalphabeta, locallinearweights
1717
export optimalbandwidth, leaveoneoutCV, optimizeAICc
1818
export npregress
19+
export npvaryingcoef, predict_varyingcoef, varyingcoefficientweights
1920

2021
include("kernels.jl")
2122
include("univariateopt.jl")
23+
include("varyingcoefficient.jl")
2224

2325

2426
########################################

0 commit comments

Comments
 (0)