File: NSextra.r

package info (click to toggle)
r-cran-wavethresh 4.6.8-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,324 kB
  • sloc: ansic: 8,159; makefile: 2
file content (259 lines) | stat: -rw-r--r-- 6,476 bytes parent folder | download | duplicates (2)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"makewpstRO" <- 
function(timeseries, response, filter.number = 10., family = 
	"DaubExPhase", trans = logabs, percentage = 10.)
{
	#
	#
	# Using the data in time series (which should be a length a power of two)
	# and the response information. Create an object
	# of class wpstRO (stationary wavelet packet regression Object).
	#
	# Given this wpstRO and another timeseries a function exists to predict
	# the group membership of each timeseries element
	#
	#
	# First build stationary wavelet packet object
	#
	#
	# Now convert this to a matrix 
	#
	twpst <- #
	wpst(timeseries, filter.number = filter.number, family = family
		)
	#
	# Now extract the ``best'' 1D variables.
	#
	tw2m <- #
	wpst2m(wpstobj = twpst, trans = trans)
	tbm <- #
	bestm(tw2m, y = response, percentage = percentage)
	#
	# Now build data frame from these variables
	#
	#
	print.w2m(tbm)
	nc <- ncol(tbm$m)
	nr <- nrow(tbm$m)
	tdf <- data.frame(response, tbm$m)
	dimnames(tdf) <- list(as.character(1.:nr), c("response", paste(
		"X", 1.:nc, sep = "")))
	l <- list(df = tdf, ixvec = tbm$ixvec, level = tbm$level, pktix
		 = tbm$pktix, nlevels = tbm$nlevels, cv = tbm$cv, 
		filter = twpst$filter, trans = trans)
	oldClass(l) <- "wpstRO"
	l
}


"wpstREGR" <- 
function(newTS, wpstRO)
{
	#
	# Extract the "best packets"
	#
	newwpst <- #
	wpst(newTS, filter.number = wpstRO$filter$filter.number, family
		 = wpstRO$filter$family)
	goodlevel <- wpstRO$level
	goodpkt <- wpstRO$pkt
	npkts <- length(goodpkt)
	ndata <- length(newTS)
	m <- matrix(0., nrow = ndata, ncol = npkts)
	J <- nlevelsWT(newwpst)
	grot <- compgrot(J, filter.number=wpstRO$filter$filter.number,
		family=wpstRO$filter$family)
	for(i in 1.:npkts) {
		j <- goodlevel[i]
		m[, i] <- guyrot(accessD(newwpst, level = j, index = 
			goodpkt[i]), grot[J - j])/(sqrt(2.)^(J - j))
		m[, i] <- wpstRO$trans(m[, i])
	}
	dimnames(m) <- list(NULL, paste("X", 1.:npkts, sep = ""))
	l <- data.frame(m)
	l
}

"wpst2m" <- 
function(wpstobj, trans = identity)
{
	#
	# Function that converts a wpstobj into a matrix
	#
	# Input:
	#
	#		wpstobj:	the wpstobj to convert
	#
	#		trans:		the transform to apply to the
	#				wpst coefficients as they come out
	#
	#				an interesting alternative is
	#				trans = log( . )^2
	#				(you'll have to write this function)
	#
	#
	# Returns: An object of class w2m
	#
	#	This is a list with the following components:
	#
	#	m	- a matrix of order ndata x nbasis
	#
	#		where ndata is the number of data points for
	#		the time series that constituted wpstobj
	#
	#		and nbasis is the number of bases in the wpstobj 
	#
	#		Each column corresponds to a basis function
	#
	#		The row ordering is the same as the time series
	#		that constituted wpstobj
	#
	#	pktix	-	a vector of length nbasis which
	#			describes the packet index of the
	#			basis function in wpstm
	#
	#	level	-	as pktix but for the level
	#
	#	nlevels	The number of levels
	#
	J <- nlev <- nlevelsWT(wpstobj)
	grot <- compgrot(J, filter.number = wpstobj$filter$filter.number,
		family = wpstobj$filter$family)
	nbasis <- 2. * (2.^nlev - 1.)
	ndata <- 2.^nlev
	m <- matrix(0., nrow = ndata, ncol = nbasis)
	level <- rep(0., nbasis)
	pktix <- rep(0., nbasis)
	cnt <- 1.
	cat("Level: ")
	for(j in 0.:(nlev - 1.)) {
		cat(j, " ")
		lcnt <- 0.
		npkts <- 2.^(nlev - j)
		prcnt <- as.integer(npkts/10.)
		for(i in 0.:(npkts - 1.)) {
			pkcoef <- guyrot(accessD(wpstobj, level = j,
				index = i), grot[J - j])/(sqrt(2.)^
				(J - j))
			m[, cnt] <- trans(pkcoef)
			level[cnt] <- j
			pktix[cnt] <- i
			lcnt <- lcnt + 1.
			cnt <- cnt + 1.
			if (prcnt > 0)	{
				if(lcnt %% prcnt == 0.) {
					lcnt <- 0.
					cat(".")
					}
				}
		}
		cat("\n")
	}
	cat("\n")
	l <- list(m = m, level = level, pktix = pktix, nlevels = J)
	oldClass(l) <- "w2m"
	l
}

"compgrot" <- 
function(J, filter.number, family)
{
	if(filter.number == 1. && family == "DaubExPhase") {
		grot <- (2.^(0.:(J - 1.)) - 1.)
	}
	else {
		grot <- (1.:J)^2.
		grot[1.] <- 2.
		grot <- cumsum(grot)
	}
	grot
}

"logabs" <- 
function(x)
logb(x^2.)

"bestm" <- 
function(w2mobj, y, percentage = 50.)
{
	#
	# Compute desired number of bases
	#
	ndata <- #
	nrow(w2mobj$m)
	#
	# Actual number of bases
	#
	dbasis <- #
	as.integer((percentage * ndata)/100.)
	nbasis <- ncol(w2mobj$m)
	cv <- rep(0., nbasis)
	for(i in 1.:nbasis) {
		cv[i] <- cor(w2mobj$m[, i], y)
	}
	cv[is.na(cv)] <- 0.
	sv <- rev(sort.list(abs(cv)))[1.:dbasis]
	ixvec <- 1.:nbasis
	l <- list(m = w2mobj$m[, sv], ixvec = ixvec[sv], pktix = w2mobj$
		pktix[sv], level = w2mobj$level[sv], nlevels = w2mobj$
		nlevels, cv = cv[sv])
	oldClass(l) <- "w2m"
	l
}

"print.w2m" <- 
function(x, maxbasis = 10., ...)
{
	w2mobj <- x
	cat("Contains SWP coefficients\n")
	cat("Original time series length: ", nrow(w2mobj$m), "\n")
	cat("Number of bases: ", ncol(w2mobj$m), "\n")
	lbasis <- min(maxbasis, ncol(w2mobj$m))
	if(is.null(w2mobj$ixvec)) {
		cat("Raw object\n")
		mtmp <- cbind(w2mobj$level[1.:lbasis], w2mobj$pktix[
			1.:lbasis])
		dimnames(mtmp) <- list(NULL, c("Level", "Pkt Index"))
	}
	else {
		cat("Some basis selection performed\n")
		mtmp <- cbind(w2mobj$level[1.:lbasis], w2mobj$pktix[
			1.:lbasis], w2mobj$ixvec[1.:lbasis], w2mobj$
			cv[1.:lbasis])
		dimnames(mtmp) <- list(NULL, c("Level", "Pkt Index",
			"Orig Index", "Score"))
	}
	print(mtmp)
	if(ncol(w2mobj$m) > maxbasis)
		cat("etc. etc.\n")
	invisible()
}

"print.wpstRO" <- 
function(x, maxbasis = 10., ...)
{
	wpstRO <- x
	cat("Stationary wavelet packet regression object\n")
	cat("Composite object containing components:")
	cat("Original time series length: ", nrow(wpstRO$df), "\n")
	cat("Number of bases: ", ncol(wpstRO$df) - 1., "\n")
	lbasis <- min(maxbasis, ncol(wpstRO$df) - 1.)
	if(is.null(wpstRO$ixvec)) {
		cat("Raw object\n")
		mtmp <- cbind(wpstRO$level[1.:lbasis], wpstRO$pktix[
			1.:lbasis])
		dimnames(mtmp) <- list(NULL, c("Level", "Pkt Index"))
	}
	else {
		cat("Some basis selection performed\n")
		mtmp <- cbind(wpstRO$level[1.:lbasis], wpstRO$pktix[
			1.:lbasis], wpstRO$ixvec[1.:lbasis], wpstRO$
			cv[1.:lbasis])
		dimnames(mtmp) <- list(NULL, c("Level", "Pkt Index",
			"Orig Index", "Score"))
	}
	print(mtmp)
	if(ncol(wpstRO$df) > maxbasis)
		cat("etc. etc.\n")
	invisible()
}