Description Usage Arguments Details Value See Also Examples
Use simple gradient descent to locate model parameters, i.e., primary effects, multiplicative effects, and activation parameters, W
.
1 2 3 4 5 6 7 8 | f_fit_gradient_01(
X,
y,
m_tot,
U = NULL,
m_start = 1,
mact_control = f_control_mactivate(),
verbosity = 2)
|
X |
Numerical matrix, |
y |
Numerical vector of length |
m_tot |
Scalar non-negative integer. Total number of columns of activation layer, |
U |
Numerical matrix, |
m_start |
Currently not used. |
mact_control |
Named list of class |
verbosity |
Scalar integer. |
Please make sure to read Details in f_dmss_dW
help page before using this function.
An unnamed list of class mactivate_fit_gradient_01
of length m_tot + 1
. Each node is a named list containing fitted parameter estimates. The first top-level node of this object contains parameter estimates when fitting ‘primary effects’ only (W
has no columns), the second, parameter estimates for fitting with 1 column of W
, and so on.
Essentially equivalent to, but likely slower than: f_fit_hybrid_01
. See f_fit_gradient_logistic_01
for logistic data (binomial response).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | xxnow <- Sys.time()
library(mactivate)
set.seed(777)
d <- 4
N <- 2000
X <- matrix(rnorm(N*d, 0, 1), N, d) ####
colnames(X) <- paste0("x", I(1:d))
############# primary effects
b <- rep_len( c(-1/2, 1/2), d )
###########
xxA <- (X[ , 1]+1/3) * (X[ , 2]-1/3)
#xxA <- (X[ , 1]+0/3) * (X[ , 2]-0/3)
ystar <-
X %*% b +
2 * xxA
m_tot <- 4
#############
xs2 <- "y ~ . "
xtrue_formula <- eval(parse(text=xs2))
xnoint_formula <- eval(parse(text="y ~ . - xxA"))
yerrs <- rnorm(N, 0, 3)
y <- ystar + yerrs
## y <- (y - mean(y)) / sd(y)
########## standardize X
Xall <- t( ( t(X) - apply(X, 2, mean) ) / apply(X, 2, sd) )
yall <- y
Nall <- N
####### fold index
xxfoldNumber <- rep_len(1:2, N)
ufolds <- sort(unique(xxfoldNumber)) ; ufolds
############### predict
############### predict
dfx <- data.frame("y"=yall, Xall, xxA)
tail(dfx)
################### incorrectly fit LM: no interactions
xlm <- lm(xnoint_formula , data=dfx)
summary(xlm)
yhat <- predict(xlm, newdata=dfx)
sqrt( mean( (yall - yhat)^2 ) )
################### correctly fit LM
xlm <- lm(xtrue_formula, data=dfx)
summary(xlm)
yhat <- predict(xlm, newdata=dfx)
sqrt( mean( (yall - yhat)^2 ) )
################ fit using gradient m-activation
######
m_tot <- 4
xcmact_gradient <-
f_control_mactivate(
param_sensitivity = 10^11,
bool_free_w = TRUE,
w0_seed = 0.05,
w_col_search = "alternate",
bool_headStart = TRUE,
ss_stop = 10^(-12), ###
escape_rate = 1.02, #### 1.0002,
Wadj = 1/1,
force_tries = 0,
lambda = 0
)
#### Fit
Uall <- Xall
head(Uall)
xthis_fold <- ufolds[ 1 ]
xndx_test <- which( xxfoldNumber %in% xthis_fold )
xndx_train <- setdiff( 1:Nall, xndx_test )
X_train <- Xall[ xndx_train, , drop=FALSE ]
y_train <- yall[ xndx_train ]
U_train <- Uall[ xndx_train, , drop=FALSE ]
xxls_out <-
f_fit_gradient_01(
X = X_train,
y = y_train,
m_tot = m_tot,
U = U_train,
m_start = 1,
mact_control = xcmact_gradient,
verbosity = 0
)
######### check test error
U_test <- Uall[ xndx_test, , drop=FALSE ]
X_test <- Xall[ xndx_test, , drop=FALSE ]
y_test <- yall[ xndx_test ]
yhatTT <- matrix(NA, length(xndx_test), m_tot+1)
for(iimm in 0:m_tot) {
yhat_fold <- predict(object=xxls_out, X0=X_test, U0=U_test, mcols=iimm )
yhatTT[ , iimm + 1 ] <- yhat_fold
}
errs_by_m <- NULL
for(iimm in 1:ncol(yhatTT)) {
yhatX <- yhatTT[ , iimm]
errs_by_m[ iimm ] <- sqrt(mean( (y_test - yhatX)^2 ))
cat(iimm, "::", errs_by_m[ iimm ])
}
##### plot test RMSE vs m
plot(0:(length(errs_by_m)-1), errs_by_m, type="l", xlab="m", ylab="RMSE Cost")
##################
xtrue_formula_use <- xtrue_formula
xlm <- lm(xnoint_formula , data=dfx[ xndx_train, ])
yhat <- predict(xlm, newdata=dfx[ xndx_test, ])
cat("\n\n", "No interaction model RMSE:", sqrt( mean( (y_test - yhat)^2 ) ), "\n")
xlm <- lm(xtrue_formula_use , data=dfx[ xndx_train, ])
yhat <- predict(xlm, newdata=dfx[ xndx_test, ])
cat("\n\n", "'true' model RMSE:", sqrt( mean( (y_test - yhat)^2 ) ), "\n")
cat( "Runtime:", difftime(Sys.time(), xxnow, units="secs"), "\n" )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.