An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110818/5277fe81/attachment.pl>
Coding question for behavioral data analysis
4 messages · Romain DOUMENC, jim holtman, jabroesch
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello,
as far as I understood your problem, this function might do the trick:
CountNextBehavior <- function (data.source, interest.behavior,
lev.ignore, interest.timeframe) {
## ------------------------------
## Returns the number of occuring behavior in a given timeframe
##
## Args:
## data.source is the source dataframe, with columns Behavior and
## Time. Behavior is assumed to be a factor and Time an integer
## interest.behavior is the seeked level of the behavior
## lev.ignore is a vector of behavior levels to ignore
## interest.timeframe fixes the time frame for observation count
##
## Returns:
## a matrix named according to the behaviors
# First, get rid of unwanted behavioral levels
data.source <- with(data.source[!data.source$Behavior %in%
lev.ignore, ],
data.frame(Time = Time,
Behavior = factor(Behavior)))
# Creates the return matrix
seeked.blevels <- levels(data.source$Behavior)
count.behavior <- matrix(rep(0,length(seeked.blevels)), nrow=1,
dimnames=list("Count", seeked.blevels))
# Look when the behavior occurs
seeked.behavior <- data.source$Behavior == interest.behavior
occuring.time <- data.source$Time[seeked.behavior]
# Iterate over occuring times
for (obs.time in occuring.time) {
# Get all the observed behavior in the given timeframe
this.timeframe <- data.source$Time > obs.time &
data.source$Time <= obs.time + interest.timeframe
this.behavior <- data.source$Behavior[this.timeframe]
# Get the level of the first observed behavior
first.behavior <- this.behavior[1]
# Count the number of occurences
this.count <- sum(this.behavior == first.behavior)
# Add the count to the given behavior
count.behavior[first.behavior] <- count.behavior[first.behavior] +
this.count
}
return(count.behavior)
}
Am 18.08.2011 19:29, schrieb jabroesch:
Hello all,
I have a question which I have been struggling with for several weeks
now, that I think might be easy for more proficient coders than
myself. I have a large behavioral dataset, with behaviors and the
times (milliseconds) that they occurred. Each subject has a separate
file, and a sample subject file can be generated using the following
syntax:
Time <- c(1000, 1050, 1100, 1500, 2500, 5000, 6500, 6600, 7000)
Behavior <- c("g", "a", "s", "5", " z", "g", "z", "g", "a")
mydata <- data.frame(Time,Behavior)
My basic goal is to be able to extract some details about what
behaviors follow another specific behavior within a time window
(say1000 milliseconds). I figured out how to determine if one specific
behavior follows another specific behavior within that window with the
following syntax.
TimeG=mydata$Time[mydata$Behavior == "g"]
TimeA=mydata$Time[mydata$Behavior == "a"]
out=rep(NA, length(TimeG))
for (i in 1:length(TimeG)){tmp = TimeA-TimeG[i]
out[i]=(sum(0 < tmp & tmp <=1000 )>0 ) }
number_of_behaviors<-length(TimeG)
number_of_affectmirroring<-sum(out)
This generates 2 values: the number of times that the target behavior
"g" occurred, and the number of times that it was followed by the
behavior "a" within 1000 milliseconds.
Question:
What I can't seem to figure out is a to generate a count of the number
of times that multiple different types of behaviors immediately follow
a specific behavior within 1000 milliseconds. So say the behavior of
interest is ???g??? as it is in the example above. I want to determine
1)what was the next behavior (from a specified list of possible
behaviors bellow) that followed it within 1000 milliseconds.
Ideally the output would 1 row with be 13 columns. The first column
would be the number of times that the target behavior, "g" in this
example occurs. The next 12 columns would be the number of times that
one of the specific behaviors was the next behavior that followed
within 1000 milliseconds. So one column for each of these behaviors :
a s d z x c v q w e r t.
The two complicating factors are: 1)there might be multiple behaviors
that followed within 1000 milliseconds, and I only want to count the
first one; and 2)there are additional behaviors that I would like to
ignore (like the "5" in the example above).
Any help or suggestions are appreciated.
Thank you,
James Broesch
--
View this message in context: http://r.789695.n4.nabble.com/Coding-question-for-behavioral-data-analysis-tp3753151p3753151.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
- -- - -- RD -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJOThLwAAoJEPy6W3H9JxwxdiUH+gItFSU2shX/82vbZdffIs6l NyGgNP6FSQBSI8zJB+jZQG/+g6s18Lf7E1idcvcY9lbaU8jwsL5cj7eeyV3mTKgq HQbDthNkvrrMofQwFbTo5m0DesRMHPzNa9H9SChxXH8hYTxX1eEzEBAtDRsEeBL6 Tx4FbYH6FLBSr7IZ2dlNlw/9QbbLVg1a1w5IkKvyDQUwKPqCtIoCnKX55JYC0CYR S3TeaxC1jJw6mMdJkO2xNsXxKvsU5zS+HC6AeK6GdKzXw76rQucUJExea2Z+tAtc uPBc9gtObP4/BsRnmv5NikCVaZNZzJg8TZZbnh2/Siw1isUSPPtm46P38EUhx94= =ruSU -----END PGP SIGNATURE-----
You might try using "outer" to create a matrix that will help out:
Time <- c(1000, 1050, 1100, 1500, 2500, 5000, 6500, 6600, 7000) Time
[1] 1000 1050 1100 1500 2500 5000 6500 6600 7000
?outer
starting httpd help server ... done
x <- outer(Time, Time, FUN = function(a, b){d <- b-a; (d>=0) & (d <= 1000)})
x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE [2,] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE [3,] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE [6,] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE [7,] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE [9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
This says, reading down the columns, that event 4 occurs after 1, 2 & 3 within the window; event 9 occurs after 7 & 8 within the window; etc.
On Thu, Aug 18, 2011 at 1:29 PM, jabroesch <james.broesch at gmail.com> wrote:
Hello all,
I have a question which I have been struggling with for several weeks
now, that I think might be easy for more proficient coders than
myself. I have a large behavioral dataset, with behaviors and the
times (milliseconds) that they occurred. Each subject has a separate
file, and a sample subject file can be generated using the following
syntax:
Time <- c(1000, 1050, 1100, 1500, 2500, 5000, 6500, 6600, 7000)
Behavior <- c("g", "a", "s", "5", " z", "g", "z", "g", "a")
mydata <- data.frame(Time,Behavior)
My basic goal is to be able to extract some details about what
behaviors follow another specific behavior within a time window
(say1000 milliseconds). I figured out how to determine if one specific
behavior follows another specific behavior within that window with the
following syntax.
TimeG=mydata$Time[mydata$Behavior == "g"]
TimeA=mydata$Time[mydata$Behavior == "a"]
out=rep(NA, length(TimeG))
for (i in 1:length(TimeG)){tmp = TimeA-TimeG[i]
out[i]=(sum(0 < tmp & tmp <=1000 )>0 ) }
number_of_behaviors<-length(TimeG)
number_of_affectmirroring<-sum(out)
This generates 2 values: the number of times that the target behavior
"g" occurred, and the number of times that it was followed by the
behavior "a" within 1000 milliseconds.
Question:
What I can't seem to figure out is a to generate a count of the number
of times that multiple different types of behaviors immediately follow
a specific behavior within 1000 milliseconds. So say the behavior of
interest is ?g? as it is in the example above. I want to determine
1)what was the next behavior (from a specified list of possible
behaviors bellow) that followed it within 1000 milliseconds.
Ideally the output would 1 row with be 13 columns. The first column
would be the number of times that the target behavior, "g" in this
example occurs. The next 12 columns would be the number of times that
one of the specific behaviors was the next behavior that followed
within 1000 milliseconds. ?So one column for each of these behaviors :
a s d z x c v q w e r t.
The two complicating factors are: 1)there might be multiple behaviors
that followed within 1000 milliseconds, and I only want to count the
first one; and 2)there are additional behaviors that I would like to
ignore (like the "5" in the example above).
Any help or suggestions are appreciated.
Thank you,
James Broesch
--
View this message in context: http://r.789695.n4.nabble.com/Coding-question-for-behavioral-data-analysis-tp3753151p3753151.html
Sent from the R help mailing list archive at Nabble.com.
? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
3 days later
Thank you both for the replies. While neither produced the exact desired
results, they spurred some new thinking about how to approach the problem. I
came up with a way to get the output desired, but it is probably pretty
clunky. I will post it here anyway, in case someone is interested in the
future.
TimeG=mydata$Time[mydata$Behavior == "g"]
TimeA=mydata$Time[mydata$Behavior == "a"]
#next line prevents errors for when there are no instances of a given
behavior by creating a blank file
ifelse( (sum(mydata$Time[mydata$Behavior == "a"])==0), TimeA<-0,
TimeA<-TimeA)
outBehavA<-data.frame(matrix(ncol =length(TimeA), nrow =length(TimeG)))
for (j in 1:length(TimeA)){
for (i in 1:length(TimeG)){
outBehavA[i,j]=TimeA[j]-TimeG[i] }}
rowmin=apply(outBehavA, 1, function(x) min(x[x>=0]))
A<-rowmin
t(A)
timedif<-data.frame(A)
TimeG=mydata$Time[mydata$Behavior == "g"]
TimeZ=mydata$Time[mydata$Behavior == "z"]
ifelse( (sum(mydata$Time[mydata$Behavior == "z"])==0), TimeZ<-0,
TimeZ<-TimeZ)
outBehavZ<-data.frame(matrix(ncol =length(TimeZ), nrow =length(TimeG)))
for (j in 1:length(TimeZ)){
for (i in 1:length(TimeG)){
outBehavZ[i,j]=TimeZ[j]-TimeG[i] }}
rowmin=apply(outBehavZ, 1, function(x) min(x[x>=0]))
Z<-rowmin
t(Z)
timedif<-cbind(Z)
#removing all values over 1000miliseconds
timedif<-as.data.frame(timedif)
timedif<-apply(timedif, c(1,2), function(x) ifelse(x > 1000,
x<-NA, x<-x))
timedif<-as.data.frame(timedif)
#then retain only minimum(first behavior)
for (i in 1:nrow(timedif)){
t<-which.min(timedif[i,])
timedif[i,t]<-1
}
timedif<-apply(timedif, c(1,2), function(x) ifelse(x ==1,
x<-x, x<-0))
timedif<-as.data.frame(timedif)
#sumarizing for each subject
number_of_target_behaviors<-nrow(timedif)
#number of times a behavior was responed to within 1000ms
rowsums1<-rowSums (timedif, na.rm = TRUE, dims = 1)
number_of_contingent_responses_across_domains<-sum(rowsums1)
#number_of_contingent_responses_in each domain
sumofcolumns<-colSums (timedif, na.rm = TRUE, dims = 1)
--
View this message in context: http://r.789695.n4.nabble.com/Coding-question-for-behavioral-data-analysis-tp3753151p3760249.html
Sent from the R help mailing list archive at Nabble.com.