1
- smooth.surp <- function (argvals , Wbin , Bmat0 , WfdPar , wtvec = NULL , conv = 1e-4 ,
1
+ smooth.surp <- function (argvals , y , Bmat0 , WfdPar , wtvec = NULL , conv = 1e-4 ,
2
2
iterlim = 50 , dbglev = 0 ) {
3
- # Smooths the relationship of Y to ARGVALS using weights in WTVEC by fitting
3
+ # Smooths the relationship of Y to ARGVALS using weights in WTVEC by fitting
4
4
# surprisal functions to a set of surprisal transforms of choice
5
5
# probabilities, where the surprisal transformation of each probability is
6
6
# W(p_m) = -log_M (p_m), m=1, ..., M,
@@ -20,7 +20,7 @@ smooth.surp <- function(argvals, Wbin, Bmat0, WfdPar, wtvec=NULL, conv=1e-4,
20
20
# surprisal values for each curve. It is assumed that
21
21
# that these argument values are common to all observed
22
22
# curves.
23
- # WBIN ... A matrix containingg the values to be fit.
23
+ # Y ... A matrix containing the values to be fit.
24
24
# This will be an NBIN by M matrix, where NBIN is the
25
25
# number of bins containing choice probabilities and M is
26
26
# the number of options in a specific question or rating
@@ -70,15 +70,14 @@ smooth.surp <- function(argvals, Wbin, Bmat0, WfdPar, wtvec=NULL, conv=1e-4,
70
70
n <- length(argvals )
71
71
onesobs <- matrix (1 ,n ,1 )
72
72
73
- # Check Wbin , an n by M-1 matrix of surprisal values.
73
+ # Check y , an n by M-1 matrix of surprisal values.
74
74
# It may not contain negative values.
75
75
76
- Wbin <- as.matrix(Wbin )
77
- Wbindim <- dim(Wbin )
78
- M <- Wbindim [2 ]
79
- if (Wbindim [1 ] != n )
80
- stop(" The length of ARGVALS and the number of rows of WBIN differ." )
81
- # if (min(Wbin) < 0) stop("WBIN contains negative values.")
76
+ y <- as.matrix(y )
77
+ ydim <- dim(y )
78
+ M <- ydim [2 ]
79
+ if (ydim [1 ] != n )
80
+ stop(" The length of ARGVALS and the number of rows of Y differ." )
82
81
83
82
# Check WfdPar and extract WBASIS, WNBASIS, Wlambda and WPENALTY.
84
83
# Note that the coefficient matrix is not used.
@@ -134,7 +133,7 @@ smooth.surp <- function(argvals, Wbin, Bmat0, WfdPar, wtvec=NULL, conv=1e-4,
134
133
135
134
# Set up list object for data required by PENSSEfun
136
135
137
- surpList <- list (argvals = argvals , Wbin = Wbin , wtvec = wtvec , Kmat = Kmat ,
136
+ surpList <- list (argvals = argvals , y = y , wtvec = wtvec , Kmat = Kmat ,
138
137
Zmat = Zmat , Phimat = Phimat , M = M )
139
138
# --------------------------------------------------------------------
140
139
# loop through variables and curves
@@ -248,12 +247,13 @@ smooth.surp <- function(argvals, Wbin, Bmat0, WfdPar, wtvec=NULL, conv=1e-4,
248
247
D2SSE <- surpResult $ D2SSE
249
248
DvecSmatDvecB <- surpResult $ DvecSmatDvecB
250
249
251
- result <- list (Wfd = Wfd , Bmat = Bmat , f = f , gvec = gvec , hmat = hmat ,
250
+ surpFd <- list (Wfd = Wfd , Bmat = Bmat , f = f , gvec = gvec , hmat = hmat ,
252
251
PENSSE = PENSSE , DPENSSE = DPENSSE , D2PENSSE = D2PENSSE ,
253
252
SSE = SSE , DSSE = DSSE , D2SSE = D2SSE ,
254
253
DvecSmatDvecB = DvecSmatDvecB )
254
+ class(surpFd ) <- ' surpfd'
255
255
256
- return (result )
256
+ return (surpFd )
257
257
}
258
258
259
259
# ------------------------------------------------------------------
@@ -266,7 +266,7 @@ surp.fit <- function(x, surpList) {
266
266
# extract objects from surpList
267
267
268
268
argvals <- surpList $ argvals
269
- Wbin <- surpList $ Wbin
269
+ y <- surpList $ y
270
270
wtvec <- surpList $ wtvec
271
271
Kmat <- surpList $ Kmat
272
272
Zmat <- surpList $ Zmat
@@ -288,7 +288,7 @@ surp.fit <- function(x, surpList) {
288
288
sumexpXmat <- as.matrix(apply(expXmat ,1 ,sum ))
289
289
Pmat <- expXmat / (sumexpXmat %*% matrix (1 ,1 ,M ))
290
290
Smat <- - Xmat + (log(sumexpXmat ) %*% matrix (1 ,1 ,M ))/ logM
291
- Rmat <- Wbin - Smat
291
+ Rmat <- y - Smat
292
292
vecBmat <- matrix (Bmat ,K * (M - 1 ),1 ,byrow = TRUE )
293
293
vecRmat <- matrix (Rmat ,n * M , 1 ,byrow = TRUE )
294
294
vecKmat <- kronecker(diag(rep(1 ,M - 1 )),Kmat )
0 commit comments