Skip to content

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:
- -- 
- --
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:
[1] 1000 1050 1100 1500 2500 5000 6500 6600 7000
starting httpd help server ... done
[,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:

  
    
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.