GGIR(Actigraphy)技术咨询:代码查看与结果优化相关问题
Hey there! Let's break down your GGIR questions with practical, actionable solutions— I’ve spent plenty of time troubleshooting this package for accelerometer studies, so I’ve got you covered:
Getting your hands on GGIR’s complete code is straightforward, with a few options depending on what you need:
- View source for installed functions: In R, just type any GGIR function name without parentheses (e.g.,
g.part4) to pull up its full code. To browse all R scripts in the package, runsystem.file(package = "GGIR")to get the installation path, then navigate to theRfolder inside— that’s where all the core logic lives. - Grab the latest development version: GGIR’s active development happens on GitHub. Use
devtools::install_github("wadpac/GGIR")to install the dev build, then in RStudio’s Packages pane, expand the GGIR entry to see every function. Click any function to view its source code instantly. If you want a local copy of the entire repo, just clone it directly. - Check built-in docs for context: Run
?GGIRto pull up the main package documentation, which links to detailed explanations of each module, plus example code that helps clarify how everything connects.
I’ve double-checked this against GGIR’s source code and official docs— here’s the standard color mapping for those sleep plots:
- Blue: The pre-defined sleep period window (set via parameters like
timewindowor your metadata’s "time in bed" specification) - Red: Wakefulness detected by GGIR’s sleep algorithm
- Green: Sleep detected by the algorithm
- Yellow/light green: Uncertain sleep/wake status (usually when there’s too little movement data to make a confident call)
If you want to verify this yourself, look into the plot_sleep.R script in GGIR’s source code— it explicitly defines these color mappings.
Both of these tweaks are totally doable with either parameter settings or quick pre/post-processing:
- Displaying accurate subject IDs:
- GGIR defaults to using accelerometer filenames as IDs, so if your filenames are already valid subject IDs, you’re good to go.
- To use custom IDs from a metadata file, specify the
metadatapathparameter when callingGGIR()org.part4(). Make sure your metadata has an ID column (default is column 1; adjust withidcolif needed) that matches your filenames, and GGIR will pull those IDs into all outputs.
- Sorting output in ascending order:
- Pre-processing fix: Before passing files to GGIR, sort your file list by ID. For example:
Then use this sortedfile_list <- sort(list.files(path = "your_accel_data_folder", pattern = ".cwa", full.names = TRUE))file_listas thedatapathinput— GGIR will process and output results in this order. - Post-processing fix: If you already have results, load the output RData file (e.g.,
load("part4_results.RData")) and usedplyr::arrange()to sort by ID:library(dplyr) sorted_results <- arrange(part4_results, ID) - Built-in parameter: GGIR’s main
GGIR()function has asortbyidparameter— set this toTRUEto automatically sort all outputs by subject ID in ascending order.
- Pre-processing fix: Before passing files to GGIR, sort your file list by ID. For example:
内容的提问来源于stack exchange,提问作者NikoSchool

