eegsmooth {eegkit} | R Documentation |
Spatial and/or Temporal Smoothing of EEG Data
Description
Smooths single- or multi-channel electroencephalography (EEG) with respect to space and/or time. Uses the bigspline
, bigtps
, and bigssa
functions (from bigsplines
package) for smoothing.
Usage
eegsmooth(voltage, space = NULL, time = NULL, nknots = NULL,
rparm = NULL, lambdas = NULL, skip.iter = TRUE,
se.fit = FALSE, rseed = 1234)
Arguments
voltage |
Vector of recorded EEG voltage at each row in |
space |
Matrix of electrode coordinates (in three-dimensions) at which EEG was recorded. If |
time |
Vector of time points at which EEG was recorded. If |
nknots |
Number of knots to sample for smoothing. Positive integer. |
rparm |
Rounding parameter(s) to use for smoothing. See Notes and Examples. |
lambdas |
Smoothing parameter(s) to use for smoothing. |
skip.iter |
If |
se.fit |
If |
rseed |
Random seed to use for knot selection. Set |
Value
For temporal smoothing only: an object of class "bigspline" (see bigspline
).
For spatial smoothing only: an object of class "bigtps" (see bigtps
).
For spatial-temporal smoothing: an object of class "bigssa" (see bigssa
).
Note
For temporal smoothing only (i.e., space=NULL
), the input rparm
should be a positive scalar less than 1. Larger values produce faster (but less accurate) approximations. Default is 0.01, which I recommend for temporal smoothing; rparm=0.005
may be needed for particuarly rough signals, and rparm=0.02
could work for smoother signals.
For spatial smoothing only (i.e., time=NULL
), the input rparm
should be a positive scalar giving the rounding unit for the spatial coordinates. For example, rparm=0.1
rounds each coordinate to the nearest 0.1 (same as round(space,1)
).
For spatial-temporal smoothing (i.e., both space
and time
are non-null), the input rparm
should be a list of the form rparm=list(space=0.1,time=0.01)
, where the 0.1 and 0.01 can be replaced by your desired rounding parameters.
Setting rparm=NA
will use the full data solution; this is more computationally expensive, and typically produces a solution very similar to using rparm=0.01
(see references).
Author(s)
Nathaniel E. Helwig <helwig@umn.edu>
References
Helwig, N. E. (2013). Fast and stable smoothing spline analysis of variance models for large samples with applications to electroencephalography data analysis. Unpublished doctoral dissertation. University of Illinois at Urbana-Champaign.
Helwig, N.E. (2015). bigsplines: Smoothing Splines for Large Samples. http://CRAN.R-project.org/package=bigsplines
Helwig, N. E. & Ma, P. (2015). Fast and stable multiple smoothing parameter selection in smoothing spline analysis of variance models with large samples. Journal of Computational and Graphical Statistics, 24(3), 715-732.
Helwig, N. E. & Ma, P. (2016). Smoothing spline ANOVA for super large samples: Scalable computation via rounding parameters. Statistics and Its Interface, 9(4), 433-444.
Examples
########## EXAMPLE 1: Temporal ##########
# get "PZ" electrode of "c" subjects in "eegdata" data
data(eegdata)
idx <- which(eegdata$channel=="PZ" & eegdata$group=="c")
eegdata <- eegdata[idx,]
# temporal smoothing
eegmod <- eegsmooth(eegdata$voltage,time=eegdata$time)
# define data for prediction
time <- seq(min(eegdata$time),max(eegdata$time),length.out=100)
yhat <- predict(eegmod,newdata=time,se.fit=TRUE)
# plot results using eegtime
eegtime(time*1000/255,yhat$fit,voltageSE=yhat$se.fit,ylim=c(-4,4),main="Pz")
########## EXAMPLE 2: Spatial ##########
# get time point 65 (approx 250 ms) of "c" subjects in "eegdata" data
data(eegdata)
idx <- which(eegdata$time==65L & eegdata$group=="c")
eegdata <- eegdata[idx,]
# remove ears, nose, and reference (Cz)
idx <- c(which(eegdata$channel=="X"),which(eegdata$channel=="Y"),
which(eegdata$channel=="nd"),which(eegdata$channel=="Cz"))
eegdata <- eegdata[-idx,]
# match to eeg coordinates
data(eegcoord)
cidx <- match(eegdata$channel,rownames(eegcoord))
# spatial smoothing
eegmod <- eegsmooth(eegdata$voltage,space=eegcoord[cidx,1:3])
# use dense cap for prediction
mycap <- levels(factor(eegdata$channel))
ix <- eegcapdense(mycap,type="2d",index=TRUE)
data(eegdense)
space <- eegdense[ix,1:3]
yhat <- predict(eegmod,newdata=space)
# plot results using eegspace
#eegspace(space,yhat)
eegspace(eegdense[ix,4:5],yhat)
########## EXAMPLE 3: Spatial-Temporal (not run) ##########
# # get "c" subjects of "eegdata" data
# data(eegdata)
# idx <- which(eegdata$group=="c")
# eegdata <- eegdata[idx,]
# # remove ears, nose, and reference (Cz)
# idx <- c(which(eegdata$channel=="X"),which(eegdata$channel=="Y"),
# which(eegdata$channel=="nd"),which(eegdata$channel=="Cz"))
# eegdata <- eegdata[-idx,]
# # match to eeg coordinates
# data(eegcoord)
# cidx <- match(eegdata$channel,rownames(eegcoord))
# # spatial-temporal smoothing
# eegmod <- eegsmooth(eegdata$voltage,space=eegcoord[cidx,1:3],time=eegdata$time)
# # time main effect
# newdata <- list(time=seq(min(eegdata$time),max(eegdata$time),length.out=100))
# yhat <- predict(eegmod,newdata=newdata,se.fit=TRUE,include="time")
# eegtime(newdata$time,yhat$fit,voltageSE=yhat$se.fit,ylim=c(-2,4),main="Time Main Effect")
# # space main effect
# mycap <- levels(factor(eegdata$channel))
# ix <- eegcapdense(mycap,type="2d",index=TRUE)
# data(eegdense)
# newdata <- list(space=eegdense[ix,1:3])
# yhat <- predict(eegmod,newdata=newdata,include="space")
# eegspace(newdata$space,yhat)
# # interaction effect (spatial map at time point 65)
# newdata <- list(space=eegdense[ix,1:3],time=rep(65,nrow(eegdense[ix,])))
# yhat <- predict(eegmod,newdata=newdata,include="space:time")
# eegspace(newdata$space,yhat)
# # full prediction (spatial map at time point 65)
# newdata <- list(space=eegdense[ix,1:3],time=rep(65,nrow(eegdense[ix,])))
# yhat <- predict(eegmod,newdata=newdata)
# eegspace(newdata$space,yhat)