Skip to content

Commit

Permalink
Further tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
sokbae committed Oct 31, 2023
1 parent cf20973 commit 344c915
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/testthat/test-sgd_lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ test_that("sgd_lm burn matters 2", {
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_lm with scalar X", {
n = 1e05
p = 2
Expand All @@ -100,3 +101,16 @@ test_that("sgd_lm with scalar X", {
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_lm with scalar X with/out intercept", {
n = 1e05
p = 2
bt0 = c(0,5)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_lm(y~., data=my.dat, intercept=TRUE)
out2 = sgd_lm(y~., data=my.dat, intercept=FALSE)
check = max(abs(out1$coefficients[-1] - out2$coefficients))
expect_false(check==0)
})
116 changes: 116 additions & 0 deletions tests/testthat/test-sgd_qr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
test_that("sgd_qr studentize matters", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat, studentize=TRUE)
out2 = sgd_qr(y~., data=my.dat, studentize=FALSE)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_qr no_studentize matters", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat)
out2 = sgd_qr(y~., data=my.dat, no_studentize=1e06)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_qr intercept matters 1", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat)
out2 = sgd_qr(y~., data=my.dat, intercept=FALSE)
check = max(abs(out1$coefficients[-1] - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_qr intercept matters 2", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat, studentize=FALSE)
out2 = sgd_qr(y~., data=my.dat, studentize=FALSE, intercept=FALSE)
check = max(abs(out1$coefficients[-1] - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_qr bt_start matters", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat)
out2 = sgd_qr(y~., data=my.dat, bt_start=bt0)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_qr burn matters 1", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat)
out2 = sgd_qr(y~., data=my.dat, burn=100)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_qr burn matters 2", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat, studentize=FALSE)
out2 = sgd_qr(y~., data=my.dat, burn=100, studentize=FALSE)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_qr with scalar X", {
n = 1e05
p = 2
bt0 = rep(5,2)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat, studentize=TRUE)
out2 = sgd_qr(y~., data=my.dat, studentize=FALSE)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgd_qr with scalar X with/out intercept", {
n = 1e05
p = 2
bt0 = c(0,5)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgd_qr(y~., data=my.dat, intercept=TRUE)
out2 = sgd_qr(y~., data=my.dat, intercept=FALSE)
check = max(abs(out1$coefficients[-1] - out2$coefficients))
expect_false(check==0)
})
39 changes: 39 additions & 0 deletions tests/testthat/test-sgdi_lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,42 @@ test_that("Run the code with an option studentize=F",
expect_true(check<1e-2)
}
)

test_that("sgdi_lm no_studentize matters", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_lm(y~., data=my.dat)
out2 = sgdi_lm(y~., data=my.dat, no_studentize=1e06)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgdi_lm with scalar X", {
n = 1e05
p = 2
bt0 = rep(5,2)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_lm(y~., data=my.dat, studentize=TRUE)
out2 = sgdi_lm(y~., data=my.dat, studentize=FALSE)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgdi_lm with scalar X with/out intercept", {
n = 1e05
p = 2
bt0 = c(0,5)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_lm(y~., data=my.dat, intercept=TRUE)
out2 = sgdi_lm(y~., data=my.dat, intercept=FALSE)
check = max(abs(out1$coefficients[-1] - out2$coefficients))
expect_false(check==0)
})
79 changes: 79 additions & 0 deletions tests/testthat/test-sgdi_qr.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,82 @@ test_that("Different significance levels",
expect_true(check>0)
}
)

test_that("sgdi_qr no_studentize matters", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_qr(y~., data=my.dat)
out2 = sgdi_qr(y~., data=my.dat, no_studentize=1e06)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgdi_qr with scalar X", {
n = 1e05
p = 2
bt0 = rep(5,2)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_qr(y~., data=my.dat, studentize=TRUE)
out2 = sgdi_qr(y~., data=my.dat, studentize=FALSE)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgdi_qr with scalar X with/out intercept", {
n = 1e05
p = 2
bt0 = c(0,5)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_qr(y~., data=my.dat, intercept=TRUE)
out2 = sgdi_qr(y~., data=my.dat, intercept=FALSE)
check = max(abs(out1$coefficients[-1] - out2$coefficients))
expect_false(check==0)
})

test_that("sgdi_qr burn matters 1", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_qr(y~., data=my.dat)
out2 = sgdi_qr(y~., data=my.dat, burn=100)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})

test_that("sgdi_qr burn matters 2", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = 2*matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_qr(y~., data=my.dat, studentize=FALSE)
out2 = sgdi_qr(y~., data=my.dat, burn=100, studentize=FALSE)
check = max(abs(out1$coefficients - out2$coefficients))
expect_false(check==0)
})


test_that("sgdi_qr path_index matters", {
n = 1e05
p = 5
bt0 = rep(5,p)
x = matrix(rnorm(n*(p-1)), n, (p-1))
y = cbind(1,x) %*% bt0 + rnorm(n)
my.dat = data.frame(y=y, x=x)
out1 = sgdi_qr(y~., data=my.dat, path=TRUE, path_index=1)
out2 = sgdi_qr(y~., data=my.dat, path=TRUE, path_index=2)
check = max(abs(out1$beta_hat_path[100] - out2$beta_hat_path[100]))
expect_false(check==0)
})

0 comments on commit 344c915

Please sign in to comment.