[R] fminsearch usage

2013-10-02 Thread Edward Patzelt
R Help -

I'm attempting to use fminsearch and it continues to give me errors. I have
a normal Rescorla Wagner function that has 2 parameters and returns total
error. for some reason fminsearch keeps saying the type of errors below,
I've put the function and data after it using dput

 sol - fminsearch(rescorlaWagner(learningRate,decision_slope), c(-1.2,1))
Error in this$fun(x = x, index = index, fmsfundata = this$costfargument) :
  attempt to apply non-function
 sol - fminsearch(rescorlaWagner,c(alphaPlus = alphaPlus, decision_slope
= decision_slope), c(-1.2,1))
Error in options[[name]] : subscript out of bounds


rescorlaWagner - function(alphaPlus, decision_slope){

# alphaPlus - parameters[1]

# decision_slope _- parameters[2]

#dat - mainData[mainData$participants == subjects[s],]

 dat$choice - ifelse(dat$cueResp.keys == 5, 1, 0)

 dat$reward - dat$trialChange

# numberTrials[s] - length(dat$choice)

 numberTrials - length(dat$choice)


 V1 - 0; V0 - 0; totError - 0; thList = 0; block = 0; valueOne -
0; valueZero
- 0; probOfStim1 - .01; probOfStim2 - .01; logLike - 0;


V1[1] = 0; V0[1] = 0;


for (i in 2:length(dat$choice))

{



  if (dat$choice[i-1] == 1) # Chose pattern 1 last time

  {

  logLike[i] - logLike[i-1] - log(probOfStim1[i-1])

  #print(logLike)

  delta1 - dat$reward[i] - V1[i-1]

  V1[i] - V1[i-1] + alphaPlus * delta1


  V0[i] - V0[i-1]

  }

  else # Chose pattern 0 last time instead of pattern 1

  {

   logLike[i] - logLike[i-1] - log(probOfStim2[i-1])

  delta0 - dat$reward[i] - V0[i-1]

  V0[i] - V0[i-1] + alphaPlus * delta0


   V1[i] - V1[i-1]

  }



  probOfStim1[i] - exp(decision_slope * V1[i])/(exp(decision_slope * V1
[i] + exp(decision_slope * V0[i])))

  probOfStim2[i] - exp(decision_slope * V0[i])/(exp(decision_slope * V1
[i] + exp(decision_slope * V0[i])))


  valueOne[i] - V1[i]

  valueZero[i] - V0[i]



  newList - list(EVpattern1 = valueOne, EVpattern0 =
valueZero, ProbabilityStim1
= probOfStim1, ProbabilityStim2 = probOfStim2, LogLikelihood = logLike)

  logLike[i]



}


#return(newList)

return(logLike[i])


 }



dput(dat)

structure(list(highCueImg = c(1.bmp, 1.bmp, 1.bmp, 1.bmp,

1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp,

1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp,

1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp,

1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp,

1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp, 1.bmp,

1.bmp), fixationDuration = c(2.900504833, 2.137709799, 3.314149929,

3.034789701, 3.44018723, 3.559188201, 4, 3.397855819, 2.424957014,

3.010173792, 2.754706831, 2.183118953, 3.328269233, 3.058698132,

3.030312482, 2.948379082, 2.48854601, 2.635627291, 2.62972477,

3.07140, 3.343837986, 3.452638681, 3.044377797, 2, 3.139145494,

2.821634126, 3.463414658, 2.974553193, 2.887472182, 3.276110294,

2.774994095, 2.538595411, 2, 2, 2, 2.937451712, 3.030693282,

3.533087478, 2.821526274, 2), condition = c(gain, gain, gain,

gain, gain, gain, gain, gain, gain, gain, gain,

gain, gain, gain, gain, gain, gain, gain, gain,

gain, gain, gain, gain, gain, gain, gain, gain,

gain, gain, gain, gain, gain, gain, gain, gain,

gain, gain, gain, gain, gain), value = c(1L, 1L, 1L,

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,

1L, 1L, 1L, 1L, 1L), lowCueImg = c(2.bmp, 2.bmp, 2.bmp,

2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp,

2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp,

2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp,

2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp,

2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp, 2.bmp,

2.bmp, 2.bmp), trials.thisRepN = c(0L, 0L, 0L, 0L, 0L, 0L,

0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,

0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,

0L, 0L), trials.thisTrialN = c(0L, 2L, 4L, 6L, 10L, 12L, 14L,

15L, 18L, 19L, 21L, 24L, 30L, 36L, 0L, 6L, 9L, 10L, 12L, 15L,

16L, 22L, 27L, 31L, 33L, 36L, 37L, 0L, 3L, 4L, 6L, 7L, 8L, 11L,

14L, 23L, 30L, 32L, 33L, 35L), trials.thisN = c(0L, 2L, 4L, 6L,

10L, 12L, 14L, 15L, 18L, 19L, 21L, 24L, 30L, 36L, 0L, 6L, 9L,

10L, 12L, 15L, 16L, 22L, 27L, 31L, 33L, 36L, 37L, 0L, 3L, 4L,

6L, 7L, 8L, 11L, 14L, 23L, 30L, 32L, 33L, 35L), trials.thisIndex = c(0L,

2L, 4L, 6L, 10L, 12L, 14L, 15L, 18L, 19L, 21L, 24L, 30L, 36L,

0L, 6L, 9L, 10L, 12L, 15L, 16L, 22L, 27L, 31L, 33L, 36L, 37L,

0L, 3L, 4L, 6L, 7L, 8L, 11L, 14L, 23L, 30L, 32L, 33L, 35L), high = c(5L,

5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,

5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,

5L, 5L, 5L, 5L, 5L, 5L, 5L), low = c(6L, 6L, 6L, 6L, 6L, 6L,

6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,

6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,

6L, 6L), cueTime = c(0.119068770004, 20.0149482278, 39.3913808842,


[R] Constructing a matrix of outputs from loop

2013-07-08 Thread Edward Patzelt
R -

I would like to construct a matrix from the output of a loop that has 2
values it varies over the course of the loop creating a 20x20 matrix of
output values:

 ap = logspace(-3, 0, 20)

 am = logspace(-3, .7, 20)
  for (ap in apList)
  {
   for (am in amList)
   {

 output = func(ap, am)
}
}

i.e. cell 1x1 is -3,-3 and the value is 45 or something

-- 
*Edward H Patzelt | Research Assistant
Psychology | University of Minnesota  | Elliott Hall, 75 East River Road |
Minneapolis, MN 55455
Email: patze...@umn.edu  |
Main: 612.626.0072
|
Mobile: 651.315.3410
| Office: S355
**

*

[[alternative HTML version deleted]]

__
R-help@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.


[R] Changing Order of Factor Levels in Mixed Model (nlme)

2013-05-14 Thread Edward Patzelt
R Help -

Why is that in the results below, changing the order of the factor
(trialType2: levels - DD, SD, DS, SS) changes the estimates in the fixed
effects tests?

 tmp.dat4$trialType2 - sort(tmp.dat4$trialType, decreasing = TRUE)
 mod2c - lme(proportion.down ~ trialType2, data = tmp.dat4, random = ~ 1
| subject, na.action = na.omit, method = ML)
 summary(mod2c)
Linear mixed-effects model fit by maximum likelihood
 Data: tmp.dat4
   AIC  BIClogLik
  27.92306 48.23003 -7.961531

Random effects:
 Formula: ~1 | subject
(Intercept)  Residual
StdDev:   0.3800017 0.1530272

Fixed effects: proportion.down ~ trialType2
  Value  Std.Error  DF   t-value p-value
(Intercept)   0.6788875 0.08613476 141  7.881690  0.
trialType2DS  0.0287062 0.11267357 141  0.254773  0.7993
trialType2SD -0.0197194 0.12142018 141 -0.162406  0.8712
trialType2SS -0.0941918 0.12204707 141 -0.771766  0.4415
 Correlation:
 (Intr) trT2DS trT2SD
trialType2DS -0.658
trialType2SD -0.709  0.467
trialType2SS -0.706  0.464  0.571

Standardized Within-Group Residuals:
Min  Q1 Med  Q3 Max
-3.67176824 -0.22090663  0.08677971  0.14331603  2.86301670

Number of Observations: 218
Number of Groups: 74
 tmp.dat4$trialType2 - sort(tmp.dat4$trialType, decreasing = FALSE)
 mod2c - lme(proportion.down ~ trialType2, data = tmp.dat4, random = ~ 1
| subject, na.action = na.omit, method = ML)
 summary(mod2c)
Linear mixed-effects model fit by maximum likelihood
 Data: tmp.dat4
   AIC  BIClogLik
  27.92306 48.23003 -7.961531

Random effects:
 Formula: ~1 | subject
(Intercept)  Residual
StdDev:   0.3800017 0.1530272

Fixed effects: proportion.down ~ trialType2
 Value  Std.Error  DF  t-value p-value
(Intercept)  0.5846957 0.08646554 141 6.762181  0.
trialType2DS 0.0744724 0.1123 141 0.660347  0.5101
trialType2SD 0.1228981 0.12175741 141 1.009368  0.3145
trialType2SS 0.0941918 0.12204707 141 0.771766  0.4415
 Correlation:
 (Intr) trT2DS trT2SD
trialType2DS -0.660
trialType2SD -0.710  0.469
trialType2SS -0.708  0.468  0.573

Standardized Within-Group Residuals:
Min  Q1 Med  Q3 Max
-3.67176824 -0.22090663  0.08677971  0.14331603  2.86301670

Number of Observations: 218
Number of Groups: 74

-- 
*Edward H Patzelt | Research Assistant
Psychology | University of Minnesota  | Elliott Hall, 75 East River Road |
Minneapolis, MN 55455
Email: patze...@umn.edu  |
Main: 612.626.0072
|
Mobile: 651.315.3410
| Office: S355
**

*

[[alternative HTML version deleted]]

__
R-help@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.


[R] Aggregate Table Data into Cell Frequencies

2012-10-31 Thread Edward Patzelt
R-help -

I have this set of aggregated tables (sample data below via dput()).  And I
would like to have delayValue as the column variables with the temp
(temp1, temp2, temp3) values as the row variables.  However I would like to
have the temp variables *aggregated into single rows* so that I have the
frequency (Freq | counts) of each time each delayValue occurs in the
cells.

I've tried this command without luck, it seems to be dropping a bunch of
values.

tmp - reshape(sampleData, direction = wide,timevar = delayValue,
new.row.names = unique(sampleData$id) )



structure(list(delayValue = c(0, NA, 7, 8, 9, 10, NA,
NA, 4, 5, 6, 7, 8, NA, NA, 4, 5, NA, 6, 7,
8, 9, 10, 11, 12, NA, NA, 5, 6, 7, 8, 9,
NA, 4, 5, 6, 7, 8, 9, 10, NA, 9, 10, NA, NA,
NA, 4, 5, 6, NA, 5, 6, 7, NA, 7, 8, 9, 10,
11, NA, NA, 6, NA, 6, NA, 6, NA, 6, NA, 6, NA, 6,
NA, 6, NA, 6, NA, 6, NA, 6, NA, 6, NA, 6, NA, 6,
NA, 6, NA, 6, NA, 6, NA, 6, NA, 6, NA, 6, NA, NA,
6, NA, 4, 7, NA, 4, NA, 2, 3, 4, NA, 5, NA, 5,
6, NA, 6, NA, 6, NA, 6, NA, 2, 3, NA, 5, NA, 3,
4, NA, 4, 5, NA, 4, NA, 3, NA, NA, 2, 3, 4, 5,
6, 7, 8, 9, NA, 9, NA, NA, 2, NA, 3, 4, 5,
6, NA, 4, 5, 6, NA, 5, NA, 2, 3, NA, 4, 5,
6, NA, 3, NA, NA, 2, NA, 2, NA, 6, NA, 2, NA, 4,
NA, 2, 3, NA, 2, NA, 2, 3, 4, NA, 4, 5, NA, 2,
NA, 3, 4, NA, NA, 18, 19, 20, 21, 22, 23, 24,
25, 26, NA, NA, NA, NA, NA, 4, NA, NA, NA, NA, 5, 6,
NA, NA, 7, 8, 9, 10, NA, 11, 12, 13, 14, NA,
10, NA, 11, 12, 13, 14, 15, 16, NA, 10, 11,
12, NA, 13, 14, 15, 16, 17, NA, 2, NA, 2, 3,
4, NA, 2, NA, 3, NA, 2, 3, 4, 5, 6, 5, 6,
7, NA, 6, 8, NA, 7, 8, NA, 6, 7, 8, 9, NA,
NA, 6, 7, NA, NA, 5, 6, NA, 4, 5, 6, 7, NA, 6,
NA, 7, 8, NA, 6, 7, 8, NA, 5, NA, 2, 3, 4,
5, NA, 7, 7, NA, 7, NA, 7, NA, 7, NA, 7, NA, 7,
NA, 7, 8, NA, 7, 8, NA, 5, 6, NA, 4, NA, 7, NA,
4, NA, 7, NA, 7, 8, NA, 8, NA, 7, 8, NA, 7, NA,
7, NA, 7, NA, NA, 16, 17, 18, NA, 19, 20, 21,
NA, 21, 22, 23, 24, 25, 25, 26, 27, 28, NA,
NA, NA, NA, NA, NA, NA, 4, 6, NA, NA, 4, 5, 6, 7,
NA, 7, 8, 9, NA, 10, 11, 12, NA, 12, 13, 14,
NA, 13, 14, NA, 12, 13, 14, NA, 15, 16, NA, NA,
2, 3, NA, 2, 3, NA, 4, 5, 6, NA, 5, 6, 7,
8, 9, 10, 11, 12, NA, 9, 10, 11, NA, 12, 13,
NA, 9, 10, 11, NA, 12, 13, NA, 13, 14, 15, 16,
17, NA, 6, 7, NA, NA, 6, 7, 8, NA, 9, 10, 11,
NA, NA, 5, 7, NA, 5, NA, NA, 2, 3, 4, 5, NA, NA,
8, 9, NA, 8, 9, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6,
NA, NA, 6, 7, 8, NA, 8, 9, NA, 9, NA, 9, 10,
NA, 9, 10, NA, 9, 10, NA, 9, 10, NA, 7, 2, 3,
NA, 2, NA, 2, 3, NA, 2, 3, 4, NA, 2, 3, NA, 2,
3, NA, 3, NA, 2, NA, 3, 4, NA, 3, 4, NA, 5, NA,
2, 3, 4, NA, 3, 5, NA, 3, 4, NA, 2, 3, 4,
NA, 2, 3, NA, 2, 3, 4, NA, 2, NA, 3, 4, NA, NA,
18, 19, 20, 21, NA, 21, 22, 23, 24, NA, 23,
25, NA, 23, 24, 25, 26, NA, NA, 22, 23, 24, NA,
NA, NA, NA, NA, 6, 7, NA, NA, 7, 8, 9, 10, NA, 11,
12, 13, NA, 14, 15, NA, 13, 14, 15, NA, 14, 15,
16, NA, 15, 16, 17, 18, NA, 16, 17, NA, 7, 20,
21, 22, 23, NA, 22, 23, 24, NA, 25, 26, 27,
NA, 27, 28, 29, 30, NA, 31, NA, NA, NA, NA, NA, NA,
7, 8, NA, NA, 8, NA, 8, 9, NA, 10, 11, 12, 13,
NA, 13, 14, 15, NA, 15, 16, 17, NA, 15, 16, NA,
17, 18, 19, NA, NA, 12, 13, 14, NA, 13, NA, 9,
10, 11, NA, 12, 13, 14, 15, NA, NA, 9, 10, NA,
NA, NA, NA, NA, 6, NA, NA, 4, 5, 6, NA, 3, 4, 5,
NA, 6, 7, 8, NA, 9, NA, 3, 4, 5, NA, 6, 7,
8, 9, 10, 11, 12, 13, NA, 7, 13, NA, 11, 12,
NA, 13, 14, 15, 16, 17, 14, NA, 13, 15, NA, 14,
15, 16, NA, NA, NA, NA, NA, 6, 7, NA, NA, 7, NA, 7,
NA, 8, NA, 8, 9, 10, NA, 10, 11, 12, 13, NA,
13, 14, 15, 16, NA, 14, NA, NA, 2, NA, 2, NA, 2,
NA, 2, 3, NA, 2, 3, NA, 2, NA, 2, NA, 2, 3, NA,
2, 3, NA, 4, 5, NA, 5, 6, NA, 4, 5, NA, 4,
NA, 2, NA, 2, NA, 2, 3, NA, 2, 3, NA, 3, NA, 2,
NA, NA, 4, 5, 6, NA, 6, 7, 8, NA, 8, NA, 8, NA,
3, NA, 2, NA, 2, NA, 2, NA, 2, NA, 2, 3, NA, 6,
NA, 2, NA, 3, 4, 5, 6, 7, NA, 4, 5, NA, NA, NA,
2, 3, NA, 4, NA, 3, 4, NA, 7, 2, 3, 4, 5,
6, 4, 5, NA, 2, 3, NA, 3, 4, NA, 5, 6, 7,
NA, 8, 9, 10, NA, 11, NA, 10, 12, NA, 9, 10,
NA, 10, 11, NA, 6, 7, NA, 8, 9, 10, NA, 4, 5,
6, NA, 6, NA, 3, 4, 5, NA, 2, 3, NA, 3, 4,
NA, 2, 3, NA, 3, 4, NA), Freq = c(0, 1, 1, 1, 1, 1, 1,
6, 1, 1, 1, 1, 1, 5, 5, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 5, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 2, 1, 1, 4, 5, 5, 1, 1, 1,
2, 1, 1, 1, 3, 1, 1, 1, 1, 1, 5, 1, 3, 2, 3, 3, 2, 3, 3, 2, 2,
3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, 2, 3, 3, 2,
2, 3, 3, 3, 3, 2, 2, 3, 1, 1, 4, 1, 1, 4, 1, 4, 1, 1, 2, 1, 3,
2, 1, 2, 2, 2, 3, 3, 2, 1, 4, 1, 1, 4, 2, 3, 2, 1, 3, 2, 1, 3,
2, 3, 2, 3, 5, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 2, 1, 2, 3, 1, 1,
1, 1, 2, 1, 1, 1, 2, 1, 4, 1, 1, 3, 1, 1, 1, 2, 1, 4, 5, 3, 2,
3, 3, 3, 2, 2, 4, 1, 5, 1, 1, 3, 2, 3, 2, 1, 1, 1, 1, 1, 4, 1,
4, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 5, 5, 5, 5, 1, 4,
5, 5, 6, 2, 1, 2, 6, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1,
1, 1, 1, 1, 5, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, 1, 2, 1, 2,
1, 4, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 3, 1, 1, 3, 1, 1,
1, 1, 1, 6, 2, 1, 2, 6, 1, 1, 4, 1, 1, 1, 1, 1, 2, 3, 1, 1, 3,
1, 1, 1, 3, 1, 4, 1, 1, 1, 1, 1, 1, 2, 3, 3, 3, 3, 2, 2, 3, 3,
2, 2, 3, 1, 2, 2, 1, 1, 3, 1, 

[R] Axis Breaks with ggplot2

2012-10-19 Thread Edward Patzelt
R-help -

I'm trying to create axis breaks similar to this :
http://www.r-bloggers.com/wp-content/uploads/2010/08/bar-chart-natural-axis-split1.png
.

Is there a way to do this in R?  Here's my code thus far:

structure(list(condition = structure(c(2L, 1L, 3L), .Label = c(con,
exp, unedit), class = factor), trial.avg = c(4.045833,
4.335417, 4.61875), trial.sd = c(0.928718367573187,
0.851822141963017,
1.03502368980692), s.e. = c(0.0232179591893297, 0.0212955535490754,
0.163651614601074), N = c(40, 40, 40), condition2 = structure(1:3, .Label =
c(Interaction Censured,
Control Censured, Uncensured), class = factor)), .Names =
c(condition,
trial.avg, trial.sd, s.e., N, condition2), row.names = c(NA,
-3L), class = data.frame)

library(ggplot2)
none - theme_blank()
err1$condition - as.factor(err1$condition)
censorA - ggplot() + geom_bar(aes(y = trial.avg, x =
as.factor(condition2), fill = as.factor(condition2), position = dodge),
data = err1)

censorB - censorA + geom_errorbar(aes(x = err1$condition2, ymin =
(err1$trial.avg-(err1$trial.sd/sqrt(40))), ymax = (err1$trial.avg+(err1$
trial.sd/sqrt(40))), data = err1, width = .4))

censorC - censorB + opts(panel.background = none) + opts(panel.border =
none) + opts(panel.grid.minor = none) + opts(panel.grid.major = none) +
opts(axis.line = theme_segment(colour = grey35)) + opts(background.fill =
none)


censorC + scale_y_continuous(limits = c(0,7),  expand = c(0,0), 'Rating') +
opts(legend.position = none)

Best,

-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
S355 Elliot Hall: 612-626-0072
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


[R] Softmax Action Selection

2012-06-20 Thread Edward Patzelt
R Community -

I'm attempting to apply a softmax action selection to a probability
generated by a hidden Markov model.  I'm having difficulties in how to
apply the softmax temperature parameter (beta).  Here is my code thus far.
 I'm thinking the sigmoid function will work but I need this function to
return the total error instead of the probability so I can't optimize it
with optim().

  calc.probs - function(delta,beta)
  {
# initial starting probabilities
thList = 0; block = 0
for (i in 1:length(dat$choice))
  {
  if (dat$RepNum[i] != block)
{
  pL = 0.5; pR = 0.5; block = dat$RepNum[i];
}
# Markov Transitions
  pL - pL*(1-delta) + pR*delta
  pR - 1-pL
# Apply feedback
  pflc - ifelse(dat$choice[i] == dat$reward[i], .8, .2)
  pfrc - 1 - pflc
  denom - pflc * pL + pfrc * pR
# What's the new belief given observation
  posteriorL - pflc * pL/denom
  posteriorR - 1-posteriorL
  pL - posteriorL; pR - posteriorR
# Insert softmax here
  pLlist[i] - pL

  }

  return(pLlist)
  }


-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
S355 Elliot Hall: 612-626-0072
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


[R] Running Total

2012-03-05 Thread Edward Patzelt
I'm am trying to create a vector that has a running total that adds each
time a 1 occurs.  here's the code and data

c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L,
1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L,
1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L,
0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L)


total - {};

for(i in 1:length(dat$Valid)){
total[i] - ifelse(dat$Valid[i-1]==1, total[i] + 1, total[i])
}


Cheers,



-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
S355 Elliot Hall: 612-626-0072
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Running Total

2012-03-05 Thread Edward Patzelt
Actually in looking at this I need it to only add if a 0 occurs instead of
a 1.

On Mon, Mar 5, 2012 at 12:57 PM, jim holtman jholt...@gmail.com wrote:

 cumsum

 is probably what you want:

  x - c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L,
 + 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
 + 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
 + 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L,
 + 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
 + 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L,
 + 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L,
 + 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L)
  cbind(x, cumsum(x))
   x
  [1,] 1   1
  [2,] 1   2
  [3,] 0   2
  [4,] 1   3
  [5,] 1   4
  [6,] 1   5
  [7,] 1   6
  [8,] 1   7
  [9,] 0   7
  [10,] 1   8
  [11,] 0   8
  [12,] 1   9
  [13,] 1  10
  [14,] 1  11
  [15,] 1  12
  [16,] 1  13
  [17,] 0  13


 On Mon, Mar 5, 2012 at 1:51 PM, Edward Patzelt patze...@umn.edu wrote:
  I'm am trying to create a vector that has a running total that adds each
  time a 1 occurs.  here's the code and data
 
  c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L,
  1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
  0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
  1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L,
  1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
  0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L,
  1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L,
  0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L)
 
 
  total - {};
 
  for(i in 1:length(dat$Valid)){
  total[i] - ifelse(dat$Valid[i-1]==1, total[i] + 1, total[i])
  }
 
 
  Cheers,
 
 
 
  --
  Edward H. Patzelt
  Research Assistant – TRiCAM Lab
  University of Minnesota – Psychology/Psychiatry
  VA Medical Center
  S355 Elliot Hall: 612-626-0072
  www.psych.umn.edu/research/tricam
 
 [[alternative HTML version deleted]]
 
 
  __
  R-help@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?
 Tell me what you want to do, not how you want to do it.




-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
S355 Elliot Hall: 612-626-0072
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


[R] Cubic Gradient Descent Package

2011-11-16 Thread Edward Patzelt
R -

Does anyone know of a cubic gradient descent package?  I found grad.desc()
 but that only allows for a 2d function.  I have 3 free parameters and thus
am looking for a 3d function.

Thank you,

-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
S355 Elliot Hall: 612-626-0072
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


[R] Counting Elements Conditionally

2011-08-22 Thread Edward Patzelt
R -

I have 3 variables with data below.  Variable Rev is a vector that changes
from 1 to 2, 2 to 3, etc  Variable FF is a binary variable with 1's
and 0's.  Variable bin is a different binary variable with 1's and 0's.

I want to calculate the number of elements:

1.  Starting with the first element where Rev switches (i.e. 1 to 2)

2.  The number of elements between the transition and the first 0 in the
FF vector; *when bin is also a 0.*
*
*
3.  I want to do this for each transition in Rev, but ignore all elements
after calculating #2 until another transition has occurred.


structure(list(Rev = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), FF = c(0L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L,
1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 0L, 1L, 1L, 1L), bin = c(NA, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1)), .Names = c(Rev, FF, bin
), row.names = c(NA, -40L), class = data.frame)


-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Counting Elements Conditionally

2011-08-22 Thread Edward Patzelt
Awesome, this is close, couple changes.  Below is full data set for 1
person.  I want the code to look at the first time it sees a 0 in FF after
the transition in Rev.  I then want it to test whether bin is also a 0.  If
and only if this is the first 0 in FF after the transition, and bin = 0,
then count the number of elements between the transition in Rev and the 0 in
FF.

structure(list(Rev = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), FF = c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L,
1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L,
1L, 1L), bin = c(NA, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,
1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1)), .Names = c(Rev, FF, bin), row.names = c(NA,
-125L), class = data.frame)


On Mon, Aug 22, 2011 at 3:57 PM, Jean V Adams jvad...@usgs.gov wrote:


  Re: [R] Counting Elements Conditionally
  Jean V Adams
  to:
  Edward Patzelt
  08/22/2011 03:53 PM
 
   [R] Counting Elements Conditionally
   Edward Patzelt
   to:
   r-help
   08/22/2011 02:33 PM
  
   R -
  
   I have 3 variables with data below.  Variable Rev is a vector that
  changes
   from 1 to 2, 2 to 3, etc  Variable FF is a binary variable with
  1's
   and 0's.  Variable bin is a different binary variable with 1's and
  0's.
  
   I want to calculate the number of elements:
  
   1.  Starting with the first element where Rev switches (i.e. 1 to 2)
  
   2.  The number of elements between the transition and the first 0 in
 the
   FF vector; *when bin is also a 0.*
   *
   *
   3.  I want to do this for each transition in Rev, but ignore all
  elements
   after calculating #2 until another transition has occurred.
  
  
   structure(list(Rev = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), FF = c(0L,
   1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L,
   1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L,
   1L, 1L, 1L, 0L, 1L, 1L, 1L), bin = c(NA, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
   0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1)), .Names = c(Rev, FF, bin
   ), row.names = c(NA, -40L), class = data.frame)
  
  
   --
   Edward H. Patzelt
   Research Assistant ? TRiCAM Lab
   University of Minnesota ? Psychology/Psychiatry
   VA Medical Center
   Office: S355 Elliot Hall - Twin Cities Campus
   Phone: 612-626-0072  Email: patze...@umn.edu
  
 
  Try this (I'm assuming your data.frame is called df:
 
 
  uR - unique(df$Rev)
  uR0 - uR*10L
 
  first.Rev - match(uR, df$Rev)
  first.Rev0 - match(uR0, df$Rev * 10L + df$FF)
 
  no.elements - first.Rev0 - first.Rev + 1
 
 
  This starts with Rev=1 (rather than Rev=2), but you can get rid of that
 by
  dropping the first result ...
 
 
  no.elements[-1]
 
 
  Jean
 

 Ooops.  Too hasty in my reply.  I missed the part about bin also being
 zero.  Try this instead.


 uR - unique(df$Rev)
 uR00 - uR*100L

 first.Rev - match(uR, df$Rev)
 first.Rev00 - match(uR00, df$Rev * 100L + df$FF * 10L + df$bin)

 no.elements - first.Rev00 - first.Rev + 1


 Jean




-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Counting Elements Conditionally

2011-08-22 Thread Edward Patzelt
after it sees the first occurrence of 0 in FF following a transition, I want
it to ignore all further elements until the next transition.

On Mon, Aug 22, 2011 at 3:58 PM, Edward Patzelt patze...@umn.edu wrote:

 Awesome, this is close, couple changes.  Below is full data set for 1
 person.  I want the code to look at the first time it sees a 0 in FF after
 the transition in Rev.  I then want it to test whether bin is also a 0.  If
 and only if this is the first 0 in FF after the transition, and bin = 0,
 then count the number of elements between the transition in Rev and the 0 in
 FF.

 structure(list(Rev = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L,
 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
 3L, 3L, 3L), FF = c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L,
 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L,
 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L,
 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L,
 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L,
 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L,
 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L,
 1L, 1L), bin = c(NA, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,
 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1,
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1,
 1, 1, 1, 1, 1)), .Names = c(Rev, FF, bin), row.names = c(NA,
 -125L), class = data.frame)


 On Mon, Aug 22, 2011 at 3:57 PM, Jean V Adams jvad...@usgs.gov wrote:


  Re: [R] Counting Elements Conditionally
  Jean V Adams
  to:
  Edward Patzelt
  08/22/2011 03:53 PM
 
   [R] Counting Elements Conditionally
   Edward Patzelt
   to:
   r-help
   08/22/2011 02:33 PM
  
   R -
  
   I have 3 variables with data below.  Variable Rev is a vector that
  changes
   from 1 to 2, 2 to 3, etc  Variable FF is a binary variable with
  1's
   and 0's.  Variable bin is a different binary variable with 1's and
  0's.
  
   I want to calculate the number of elements:
  
   1.  Starting with the first element where Rev switches (i.e. 1 to 2)
  
   2.  The number of elements between the transition and the first 0 in
 the
   FF vector; *when bin is also a 0.*
   *
   *
   3.  I want to do this for each transition in Rev, but ignore all
  elements
   after calculating #2 until another transition has occurred.
  
  
   structure(list(Rev = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), FF = c(0L,
   1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L,
   1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L,
   1L, 1L, 1L, 0L, 1L, 1L, 1L), bin = c(NA, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
   0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1)), .Names = c(Rev, FF, bin
   ), row.names = c(NA, -40L), class = data.frame)
  
  
   --
   Edward H. Patzelt
   Research Assistant ? TRiCAM Lab
   University of Minnesota ? Psychology/Psychiatry
   VA Medical Center
   Office: S355 Elliot Hall - Twin Cities Campus
   Phone: 612-626-0072  Email: patze...@umn.edu
  
 
  Try this (I'm assuming your data.frame is called df:
 
 
  uR - unique(df$Rev)
  uR0 - uR*10L
 
  first.Rev - match(uR, df$Rev)
  first.Rev0 - match(uR0, df$Rev * 10L + df$FF)
 
  no.elements - first.Rev0 - first.Rev + 1
 
 
  This starts with Rev=1 (rather than Rev=2), but you can get rid of that
 by
  dropping the first result ...
 
 
  no.elements[-1]
 
 
  Jean
 

 Ooops.  Too hasty in my reply.  I missed the part about bin also being
 zero.  Try this instead.


 uR - unique(df$Rev)
 uR00 - uR*100L

 first.Rev - match(uR, df$Rev)
 first.Rev00 - match(uR00, df$Rev * 100L + df$FF * 10L + df$bin)

 no.elements - first.Rev00 - first.Rev + 1


 Jean




 --
 Edward H. Patzelt
 Research Assistant – TRiCAM Lab
 University of Minnesota – Psychology/Psychiatry
 VA Medical Center
 Office: S355 Elliot Hall - Twin Cities Campus
 Phone: 612-626-0072  Email: patze...@umn.edu

 Please consider the environment before printing this email
 www.psych.umn.edu/research/tricam




-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities

Re: [R] Counting Elements Conditionally

2011-08-22 Thread Edward Patzelt
that is exactly correct, assuming we did not start at the beginning, but
started at the first transition (this is the correct way to think about it)

On Mon, Aug 22, 2011 at 4:08 PM, Jean V Adams jvad...@usgs.gov wrote:


 So, using the full data set, what should the result look like?

 c(NA, NA, NA, 3, NA,NA, NA, 2) ?

 Jean

 Edward Patzelt patze...@umn.edu wrote on 08/22/2011 03:58:38 PM:

  [image removed]
 
  Re: [R] Counting Elements Conditionally
 
  Edward Patzelt
 
  to:
 
  Jean V Adams
 
  08/22/2011 03:58 PM
 
  Cc:
 
  r-help
 
  Awesome, this is close, couple changes.  Below is full data set for
  1 person.  I want the code to look at the first time it sees a 0 in
  FF after the transition in Rev.  I then want it to test whether bin
  is also a 0.  If and only if this is the first 0 in FF after the
  transition, and bin = 0, then count the number of elements between
  the transition in Rev and the 0 in FF.
 
 
  structure(list(Rev = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
  3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L,
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
  3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
  1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
  2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
  3L, 3L, 3L), FF = c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L,
  1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L,
  1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L,
  1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L,
  1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L,
  1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
  0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L,
  1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L,
  1L, 1L), bin = c(NA, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,
  1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1)), .Names = c(Rev, FF, bin), row.names = c(NA,
  -125L), class = data.frame)
 
  On Mon, Aug 22, 2011 at 3:57 PM, Jean V Adams jvad...@usgs.gov wrote:
 
   Re: [R] Counting Elements Conditionally
   Jean V Adams
   to:
   Edward Patzelt
   08/22/2011 03:53 PM
  
[R] Counting Elements Conditionally
Edward Patzelt
to:
r-help
08/22/2011 02:33 PM
   
R -
   
I have 3 variables with data below.  Variable Rev is a vector that
   changes
from 1 to 2, 2 to 3, etc  Variable FF is a binary variable with

   1's
and 0's.  Variable bin is a different binary variable with 1's and
   0's.
   
I want to calculate the number of elements:
   
1.  Starting with the first element where Rev switches (i.e. 1 to 2)
   
2.  The number of elements between the transition and the first 0 in
 the
FF vector; *when bin is also a 0.*
*
*
3.  I want to do this for each transition in Rev, but ignore all
   elements
after calculating #2 until another transition has occurred.
   
   
structure(list(Rev = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), FF = c(0L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L,
1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 0L, 1L, 1L, 1L), bin = c(NA, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1)), .Names = c(Rev, FF, bin
), row.names = c(NA, -40L), class = data.frame)
   
   
--
Edward H. Patzelt
Research Assistant ? TRiCAM Lab
University of Minnesota ? Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu
   
  
   Try this (I'm assuming your data.frame is called df:
  
  
   uR - unique(df$Rev)
   uR0 - uR*10L
  
   first.Rev - match(uR, df$Rev)
   first.Rev0 - match(uR0, df$Rev * 10L + df$FF)
  
   no.elements - first.Rev0 - first.Rev + 1
  
  
   This starts with Rev=1 (rather than Rev=2), but you can get rid of that
 by
   dropping the first result ...
  
  
   no.elements[-1]
  
  
   Jean
  

  Ooops.  Too hasty in my reply.  I missed the part about bin also
  being zero.  Try this instead.
 
 
  uR - unique(df$Rev)
  uR00 - uR*100L
 
  first.Rev - match(uR, df$Rev)
  first.Rev00 - match(uR00, df$Rev * 100L + df$FF * 10L + df$bin)
 
  no.elements - first.Rev00 - first.Rev + 1
 
 
  Jean

[R] Indexing Permutation Values

2011-08-15 Thread Edward Patzelt
R-help -

This code iterates over a function with 2 free parameters to find a list of
values (which are the number of incorrect predictions for a computational
model).  I want to find the values of i,e when there is the minimum number
of incorrect predictions.  In other words, the value of i and e when
variable thesum is at the lowest possible number.  The bestDeltas variable
at the bottom worked with 1 free parameter.

structure(list(grid = c(3162L, 3162L, 3162L, 3162L, 3162L, 3162L,
3162L, 3162L, 3162L, 3162L, 3162L, 3162L, 3162L, 3162L, 3162L,
3162L, 3162L, 3162L, 3162L, 3162L), RepNum = c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), stimbinary = c(1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 1), choice = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1), accuracy = c(0L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 0L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 0L), actualcorrect = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1), correctstim = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), choseright = c(0,
1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1), reward = c(0L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L,
1L, 1L, 0L), resp = structure(c(5L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
5L, 3L, 5L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L), .Label = c(f11.bmp,
f13.bmp, f14.bmp, f4.bmp, f7.bmp, f9.bmp), class =
factor)), .Names = c(grid,
RepNum, stimbinary, choice, accuracy, actualcorrect,
correctstim, choseright, reward, resp), row.names = c(NA,
20L), class = data.frame)



install.packages(Hmisc, dependencies = T)
library(Hmisc)
bestDeltas= 0; betterThanChance = 0; minErr = 0
fitProp- 0
thresholded_test - 0

calc.probs - function(delta,gamma){
  # initial starting probabilities
thList = 0
block = 0
# finMatrix - 0
for (i in 1:length(dat2$choice))
{
  if (dat2$RepNum[i] != block)
  {
 pL = 0.5
 pR = 0.5
 block = dat2$RepNum[i]
 certState = 0.5
  }
  # Markov Transitions
  pL - pL*(1-delta) + pR*delta
  pR - 1-pL
  # Apply feedback
  #denom - p(F|L,C) * p(L) + p(F|R,C) * p(R)

  pflc - ifelse(dat2$choice[i] == dat2$reward[i], .8, .2)
  pfrc - 1 - pflc
  denom - pflc * pL + pfrc * pR

  # What's the new belief given observation
  posteriorL - pflc * pL/denom
  posteriorR - 1-posteriorL

  pL - posteriorL
  pR - posteriorR

  certState = gamma * certState + (1 - gamma) * pL

  thList[i] = ifelse(certState  0.55, 1, ifelse(certState  0.45, 0,
ifelse(pL  0.5, 1, 0)))
}

return(thList)
}

thesum=0
j = 1
  minx = 0.01; maxx = 0.5; incx = 0.05;
  x = seq(minx, maxx, incx)
  miny = 0.0; maxy = 1; incy = 0.05;
  y = seq(miny, maxy, incy)
for (i in x)
{
  for (e in y)

{
  thresholded = calc.probs(i, e)
  diff - abs(dat2$choice - Lag(thresholded))
  diff = ifelse(is.na(diff), 0, diff)
  thesum[j] = sum(diff, na.rm = T)
  j = j + 1
}
}
minSum = min(thesum)
minErr[s] = min(thesum)
bestDeltas[s] = min(which(thesum==minSum)) * incx + minx
# bestDeltasGammas[s] - matrix(min(which(thsum==minSum))*inc + min)
betterThanChance[s] = sum(pbinom(0:minSum, length(dat2$choice), 0.5))


-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] If find x, then y, else do nothing

2011-08-08 Thread Edward Patzelt
Thanks Josh for the code to post I have been trying to figure out how to do
that.  Your code works except that it changes subjects that responded with
1  2 to all 1's.  What does the ave argument mean in the execution of
the function?

library(car)
foo - function(x) {
 if (any(grepl(4, x))) {
   x - recode(x, 2 = 1; 4 = 2)
 }
 return(x)
}

## do it
dat$test - with(dat, *ave*(Slide1_RESP, Subject, FUN = foo))

On Fri, Aug 5, 2011 at 4:56 PM, Joshua Wiley jwiley.ps...@gmail.com wrote:

 On Fri, Aug 5, 2011 at 2:34 PM, Edward Patzelt patze...@umn.edu wrote:
 
  The problem is that we were using a task where some subjects responded
 with 1  2 and some responded with 2  4.  So there is overlap for 2
 because it means stimulus 1 for subject 1 and it means stimulus 2 for
 subject 2.
 
  subject
 
  subject_1
 
  1
 
  subject_1
 
  1
 
  subject_1
 
  1
 
  subject_1
 
  2
 
  subject_1
 
  2
 
  subject_2
 
  4
 
  subject_2
 
  2
 
  subject_2
 
  2
 
  subject_2
 
  4
 
  subject_2
 
  2
 
  subject_2
 
  2
 
  subject_2
 
  2
 
  subject_2
 
  4

 FYI providing data in the format above (this list is plain text) makes
 the job of those trying to help substantially harder.  You can use
 dput().  For example, if I wanted to share the first 10 rows of the
 built in mtcars data set, I would just copy and paste the output from
 running:

 dput(mtcars[1:10, ])

 Anyway, here you go, this should be directly executable as long as you
 have installed the 'car' package.

 ## your data in a form easily copied and pasted into the console
 ## created using dput() (highly recommended for future posts)
 dat - structure(list(subject = c(subject_1, subject_1, subject_1,
 subject_1, subject_1, subject_2, subject_2, subject_2,
 subject_2, subject_2, subject_2, subject_2, subject_2
 ), val = c(1, 1, 1, 2, 2, 4, 2, 2, 4, 2, 2, 2, 4)), .Names = c(subject,
 val), row.names = c(NA, -13L), class = data.frame)

 ## load the required package for recoding
 ## though it is overkill for only two levels
 require(car)

 ## define a function to do the recoding
 foo - function(x) {
  if (any(grepl(4, x))) {
x - recode(x, 2 = 1; 4 = 2)
  }
  return(x)
 }

 ## do it
 dat$altval - with(dat, ave(val, subject, FUN = foo))

 Cheers,

 Josh


 
  On Fri, Aug 5, 2011 at 4:25 PM, Joshua Wiley jwiley.ps...@gmail.com
 wrote:
 
  Hi Edward,
 
  You can try something like:
 
  u.ppl - unique(init.dat1$grid)
  l.ppl - ifelse(grepl(4, init.dat1$Slide1_RESP), 2,
 init.dat1$Slide1_RESP)
 
  Note that this is not exact as you have not provided a reproducible
  example.  I am not exactly sure how you are  putting 1 for 2 and 2 for
  4, if the value is equal to 4, but presumably it is clearer with data.
   In any event, look at ?ifelse it is something like a vectorized if
  statement and is, I believe, preferable to your use of a for loop.  I
  can probably give you a runnable solution if you can give the first
  few rows of the relevant data.
 
  Cheers,
 
  Josh
 
  On Fri, Aug 5, 2011 at 2:15 PM, Edward Patzelt patze...@umn.edu
 wrote:
   I want to write code that says If you find an element equal to 4 in
 this
   vector for each person in the data set tested separately, then put in
 1 for
   2 and 2 for 4, else leave the variable as is
  
u.ppl - (unique(init.dat1$grid))
l.ppl - length(u.ppl)
  for (i in 1:l.ppl)
  {
if (grep(4,init.dat1$Slide1_RESP)) {2 == 1, 4 == 2}; else
   init.dat1$Slide1_RESP
  
  }
  
   --
   Edward H. Patzelt
   Research Assistant – TRiCAM Lab
   University of Minnesota – Psychology/Psychiatry
   VA Medical Center
   Office: S355 Elliot Hall - Twin Cities Campus
   Phone: 612-626-0072  Email: patze...@umn.edu
  
   Please consider the environment before printing this email
   www.psych.umn.edu/research/tricam
  
  [[alternative HTML version deleted]]
  
  
   __
   R-help@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.
  
  
 
 
 
  --
  Joshua Wiley
  Ph.D. Student, Health Psychology
  Programmer Analyst II, ATS Statistical Consulting Group
  University of California, Los Angeles
  https://joshuawiley.com/
 
 
 
  --
  Edward H. Patzelt
  Research Assistant – TRiCAM Lab
  University of Minnesota – Psychology/Psychiatry
  VA Medical Center
  Office: S355 Elliot Hall - Twin Cities Campus
  Phone: 612-626-0072  Email: patze...@umn.edu
 
  Please consider the environment before printing this email
  www.psych.umn.edu/research/tricam



 --
 Joshua Wiley
 Ph.D. Student, Health Psychology
 Programmer Analyst II, ATS Statistical Consulting Group
 University of California, Los Angeles
 https://joshuawiley.com/




-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355

Re: [R] If find x, then y, else do nothing

2011-08-08 Thread Edward Patzelt
Here's the code.  I don't want it to even touch the vector if there are
already 1's  2's

structure(list(subject = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), resp = c(2, 1, 1, 2, 1, 2, 1,
1, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2,
1, 2)), .Names = c(subject, resp), row.names = c(1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 4161L, 4162L, 4163L, 4164L,
4165L, 4166L, 4167L, 4168L, 4169L, 4170L, 166L, 167L, 168L, 169L,
170L, 171L, 172L, 173L, 174L, 175L), class = data.frame)


R version 2.12.2 (2011-02-25)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C   LC_TIME=English_United
States.1252

attached base packages:
[1] splines   stats graphics  grDevices utils datasets
methods   base

other attached packages:
[1] car_2.0-10  nnet_7.3-1  MASS_7.3-11 Hmisc_3.8-3
survival_2.36-5 RODBC_1.3-2

loaded via a namespace (and not attached):
[1] cluster_1.13.3  grid_2.12.2 lattice_0.19-17 tools_2.12.2


On Mon, Aug 8, 2011 at 10:43 AM, Joshua Wiley jwiley.ps...@gmail.comwrote:

 On Mon, Aug 8, 2011 at 8:23 AM, Edward Patzelt patze...@umn.edu wrote:
  Thanks Josh for the code to post I have been trying to figure out how to
 do
  that.  Your code works except that it changes subjects that responded
 with
  1  2 to all 1's.  What does the ave argument mean in the execution
 of

 Not in the example data you provided on my system.  If you can provide
 data (preferablly using dput() or uploading a txt file on a file
 hosting service) that reproduces this issue, I will be happy to look
 at it for you.  You might also try reporting your sessionInfo() ---
 this may be related to the version of R or the packages you are using,
 but at present I have no information.

  the function?

 'ave' is not an argument in the function.  ave() *is* a function.  I
 call with() to have the ave() function evaluated in an environment
 created from dat (the data).  See ?ave and ?with  I could have
 equivalently (though more cumbersomely) written:

 ave(dat$Slide1_RESP, dat$Subject, FUN = foo))

 because ave will now be evaluated in the global environment, it will
 not have access to the variables stored in 'dat' unless explicitly
 told that they are in dat (as above).

 Cheers,

 Josh

  library(car)
  foo - function(x) {
   if (any(grepl(4, x))) {
 x - recode(x, 2 = 1; 4 = 2)
   }
   return(x)
  }
  ## do it
  dat$test - with(dat, ave(Slide1_RESP, Subject, FUN = foo))
  On Fri, Aug 5, 2011 at 4:56 PM, Joshua Wiley jwiley.ps...@gmail.com
 wrote:
 
  On Fri, Aug 5, 2011 at 2:34 PM, Edward Patzelt patze...@umn.edu
 wrote:
  
   The problem is that we were using a task where some subjects responded
   with 1  2 and some responded with 2  4.  So there is overlap for
 2
   because it means stimulus 1 for subject 1 and it means stimulus 2 for
   subject 2.
  
   subject
  
   subject_1
  
   1
  
   subject_1
  
   1
  
   subject_1
  
   1
  
   subject_1
  
   2
  
   subject_1
  
   2
  
   subject_2
  
   4
  
   subject_2
  
   2
  
   subject_2
  
   2
  
   subject_2
  
   4
  
   subject_2
  
   2
  
   subject_2
  
   2
  
   subject_2
  
   2
  
   subject_2
  
   4
 
  FYI providing data in the format above (this list is plain text) makes
  the job of those trying to help substantially harder.  You can use
  dput().  For example, if I wanted to share the first 10 rows of the
  built in mtcars data set, I would just copy and paste the output from
  running:
 
  dput(mtcars[1:10, ])
 
  Anyway, here you go, this should be directly executable as long as you
  have installed the 'car' package.
 
  ## your data in a form easily copied and pasted into the console
  ## created using dput() (highly recommended for future posts)
  dat - structure(list(subject = c(subject_1, subject_1, subject_1,
  subject_1, subject_1, subject_2, subject_2, subject_2,
  subject_2, subject_2, subject_2, subject_2, subject_2
  ), val = c(1, 1, 1, 2, 2, 4, 2, 2, 4, 2, 2, 2, 4)), .Names =
 c(subject,
  val), row.names = c(NA, -13L), class = data.frame)
 
  ## load the required package for recoding
  ## though it is overkill for only two levels
  require(car)
 
  ## define a function to do the recoding
  foo - function(x) {
   if (any(grepl(4, x))) {
 x - recode(x, 2 = 1; 4 = 2)
   }
   return(x)
  }
 
  ## do it
  dat$altval - with(dat, ave(val, subject, FUN = foo))
 
  Cheers,
 
  Josh
 
 
  
   On Fri, Aug 5, 2011 at 4:25 PM, Joshua Wiley jwiley.ps...@gmail.com
   wrote:
  
   Hi Edward,
  
   You can try something like:
  
   u.ppl - unique(init.dat1$grid)
   l.ppl - ifelse(grepl(4, init.dat1$Slide1_RESP), 2,
  init.dat1$Slide1_RESP)
  
   Note that this is not exact as you have not provided a reproducible
   example.  I am not exactly sure how you

Re: [R] If find x, then y, else do nothing

2011-08-08 Thread Edward Patzelt
H, I pulled out a portion of the data set to create the code for
posting.  When I execute this on the data frame I get the following for
subject 8 which is clearly incorrect.  I get this for all subjects who
originally had 1  2.

 dat$respalt - with(dat, ave(Slide1_RESP, factor(grid), FUN = foo))
head(dat$respalt,20+ ) [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


On Mon, Aug 8, 2011 at 11:14 AM, Joshua Wiley jwiley.ps...@gmail.comwrote:

 Hmm, well I suppose it technically does touch in some sense still if
 there are 1  2s, but it should just return it as is, not changed.
 Thanks for the data, very easy!  Here is what I get:

 dat - structure(list(subject = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
 8L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 6L, 6L,
 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), resp = c(2, 1, 1, 2, 1, 2, 1,
 1, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2,
 1, 2)), .Names = c(subject, resp), row.names = c(1L, 2L,
 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 4161L, 4162L, 4163L, 4164L,
 4165L, 4166L, 4167L, 4168L, 4169L, 4170L, 166L, 167L, 168L, 169L,
 170L, 171L, 172L, 173L, 174L, 175L), class = data.frame)

 require(car)
 foo - function(x) {
  if (any(grepl(4, x))) {
x - recode(x, 2 = 1; 4 = 2)
  }
  return(x)
 }

 dat$respalt - with(dat, ave(resp, factor(subject), FUN = foo))

 ## which at least for me gives:
 structure(list(subject = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
 8L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 6L, 6L,
 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), resp = c(2, 1, 1, 2, 1, 2, 1,
 1, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2,
 1, 2), respalt = c(2, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1,
 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 2)), .Names = c(subject,
 resp, respalt), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
 8L, 9L, 10L, 4161L, 4162L, 4163L, 4164L, 4165L, 4166L, 4167L,
 4168L, 4169L, 4170L, 166L, 167L, 168L, 169L, 170L, 171L, 172L,
 173L, 174L, 175L), class = data.frame)

 Does that work for you?  I am running:

 R Under development (unstable) (2011-07-30 r56564)
 Platform: x86_64-pc-mingw32/x64 (64-bit)
 with car_2.0-10

 HTH,

 Josh

 On Mon, Aug 8, 2011 at 9:02 AM, Edward Patzelt patze...@umn.edu wrote:
  Here's the code.  I don't want it to even touch the vector if there are
  already 1's  2's
 
  structure(list(subject = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
  8L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 6L, 6L,
  6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), resp = c(2, 1, 1, 2, 1, 2, 1,
  1, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2,
  1, 2)), .Names = c(subject, resp), row.names = c(1L, 2L,
  3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 4161L, 4162L, 4163L, 4164L,
  4165L, 4166L, 4167L, 4168L, 4169L, 4170L, 166L, 167L, 168L, 169L,
  170L, 171L, 172L, 173L, 174L, 175L), class = data.frame)
 
  R version 2.12.2 (2011-02-25)
  Platform: x86_64-pc-mingw32/x64 (64-bit)
 
  locale:
  [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
  States.1252LC_MONETARY=English_United States.1252
  [4] LC_NUMERIC=C   LC_TIME=English_United
  States.1252
 
  attached base packages:
  [1] splines   stats graphics  grDevices utils datasets  methods
  base
 
  other attached packages:
  [1] car_2.0-10  nnet_7.3-1  MASS_7.3-11 Hmisc_3.8-3
  survival_2.36-5 RODBC_1.3-2
 
  loaded via a namespace (and not attached):
  [1] cluster_1.13.3  grid_2.12.2 lattice_0.19-17 tools_2.12.2
 
  On Mon, Aug 8, 2011 at 10:43 AM, Joshua Wiley jwiley.ps...@gmail.com
  wrote:
 
  On Mon, Aug 8, 2011 at 8:23 AM, Edward Patzelt patze...@umn.edu
 wrote:
   Thanks Josh for the code to post I have been trying to figure out how
 to
   do
   that.  Your code works except that it changes subjects that responded
   with
   1  2 to all 1's.  What does the ave argument mean in the
 execution
   of
 
  Not in the example data you provided on my system.  If you can provide
  data (preferablly using dput() or uploading a txt file on a file
  hosting service) that reproduces this issue, I will be happy to look
  at it for you.  You might also try reporting your sessionInfo() ---
  this may be related to the version of R or the packages you are using,
  but at present I have no information.
 
   the function?
 
  'ave' is not an argument in the function.  ave() *is* a function.  I
  call with() to have the ave() function evaluated in an environment
  created from dat (the data).  See ?ave and ?with  I could have
  equivalently (though more cumbersomely) written:
 
  ave(dat$Slide1_RESP, dat$Subject, FUN = foo))
 
  because ave will now be evaluated in the global environment, it will
  not have access to the variables stored in 'dat' unless explicitly
  told that they are in dat (as above).
 
  Cheers,
 
  Josh
 
   library(car)
   foo - function(x) {
if (any(grepl(4, x))) {
  x - recode(x, 2 = 1; 4 = 2)
}
return(x)
   }
   ## do it
   dat$test - with(dat, ave(Slide1_RESP, Subject, FUN = foo))
   On Fri

Re: [R] If find x, then y, else do nothing

2011-08-08 Thread Edward Patzelt
Here's a thought, when I execute it on a data frame with only the subject
and resp variables I have the same problem.  But not on the subset of data
we've been working with.  The difference between the full vector and the
subset vector is that the full vector contains NA's.  Only 35 out of 17,000
trials, but could this screw up the function?

unique(blah$resp)

[1] 2 1 NA 4

On Mon, Aug 8, 2011 at 11:46 AM, Joshua Wiley jwiley.ps...@gmail.comwrote:

 These are shots in the dark, but you could try: running it in a clean
 R session with only the necessary packages and data loaded; testing on
 subsets of your data; examing the classes of all your variables and
 make sure they are what is expexted; upgrading your version of R.

 I just retested this on:

 R version 2.12.1 (2010-12-16)
 Platform: x86_64-pc-mingw32/x64 (64-bit)

 locale:
 [1] LC_COLLATE=English_United States.1252
 [2] LC_CTYPE=English_United States.1252
 [3] LC_MONETARY=English_United States.1252
 [4] LC_NUMERIC=C
 [5] LC_TIME=English_United States.1252

 attached base packages:
 [1] grid  splines   stats graphics  grDevices utils datasets
 [8] methods   base

 other attached packages:
 [1] car_2.0-9   survival_2.36-5 nnet_7.3-1  MASS_7.3-11

 again without issue, so I am thinking there must be something
 particular perhaps to your session going on, but it is difficult to
 say.

 On Mon, Aug 8, 2011 at 9:35 AM, Edward Patzelt patze...@umn.edu wrote:
  H, I pulled out a portion of the data set to create the code for
  posting.  When I execute this on the data frame I get the following for
  subject 8 which is clearly incorrect.  I get this for all subjects who
  originally had 1  2.
 
   dat$respalt - with(dat, ave(Slide1_RESP, factor(grid), FUN = foo))
  head(dat$respalt,20
  + )
   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 
  On Mon, Aug 8, 2011 at 11:14 AM, Joshua Wiley jwiley.ps...@gmail.com
  wrote:
 
  Hmm, well I suppose it technically does touch in some sense still if
  there are 1  2s, but it should just return it as is, not changed.
  Thanks for the data, very easy!  Here is what I get:
 
  dat - structure(list(subject = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
  8L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 6L, 6L,
  6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), resp = c(2, 1, 1, 2, 1, 2, 1,
  1, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2,
  1, 2)), .Names = c(subject, resp), row.names = c(1L, 2L,
  3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 4161L, 4162L, 4163L, 4164L,
  4165L, 4166L, 4167L, 4168L, 4169L, 4170L, 166L, 167L, 168L, 169L,
  170L, 171L, 172L, 173L, 174L, 175L), class = data.frame)
 
  require(car)
  foo - function(x) {
   if (any(grepl(4, x))) {
 x - recode(x, 2 = 1; 4 = 2)
   }
   return(x)
  }
 
  dat$respalt - with(dat, ave(resp, factor(subject), FUN = foo))
 
  ## which at least for me gives:
  structure(list(subject = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
  8L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 6L, 6L,
  6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), resp = c(2, 1, 1, 2, 1, 2, 1,
  1, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2,
  1, 2), respalt = c(2, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1,
  2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 2)), .Names = c(subject,
  resp, respalt), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
  8L, 9L, 10L, 4161L, 4162L, 4163L, 4164L, 4165L, 4166L, 4167L,
  4168L, 4169L, 4170L, 166L, 167L, 168L, 169L, 170L, 171L, 172L,
  173L, 174L, 175L), class = data.frame)
 
  Does that work for you?  I am running:
 
  R Under development (unstable) (2011-07-30 r56564)
  Platform: x86_64-pc-mingw32/x64 (64-bit)
  with car_2.0-10
 
  HTH,
 
  Josh
 
  On Mon, Aug 8, 2011 at 9:02 AM, Edward Patzelt patze...@umn.edu
 wrote:
   Here's the code.  I don't want it to even touch the vector if there
 are
   already 1's  2's
  
   structure(list(subject = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
   8L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 6L, 6L,
   6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), resp = c(2, 1, 1, 2, 1, 2, 1,
   1, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2,
   1, 2)), .Names = c(subject, resp), row.names = c(1L, 2L,
   3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 4161L, 4162L, 4163L, 4164L,
   4165L, 4166L, 4167L, 4168L, 4169L, 4170L, 166L, 167L, 168L, 169L,
   170L, 171L, 172L, 173L, 174L, 175L), class = data.frame)
  
   R version 2.12.2 (2011-02-25)
   Platform: x86_64-pc-mingw32/x64 (64-bit)
  
   locale:
   [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
   States.1252LC_MONETARY=English_United States.1252
   [4] LC_NUMERIC=C   LC_TIME=English_United
   States.1252
  
   attached base packages:
   [1] splines   stats graphics  grDevices utils datasets
  methods
   base
  
   other attached packages:
   [1] car_2.0-10  nnet_7.3-1  MASS_7.3-11 Hmisc_3.8-3
   survival_2.36-5 RODBC_1.3-2
  
   loaded via a namespace (and not attached):
   [1] cluster_1.13.3  grid_2.12.2

Re: [R] If find x, then y, else do nothing

2011-08-08 Thread Edward Patzelt
I think the problem may be there are multiple subjects with the same subject
number.  I'll have to index by subject  date.  I put together a zip file
with the original data set, the cleaned data set, and the code (with your
code integrated).  It's called clean.zip
netfiles.umn.edu/users/patze003/www/


'data.frame':   16275 obs. of  48 variables:
 $ Experiment  : chr  Rev-Fractals_2008_1
Rev-Fractals_2008_1 Rev-Fractals_2008_1 Rev-Fractals_2008_1 ...
 $ grid: int  8 8 8 8 8 8 8 8 8 8 ...
 $ Session : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Age : int  22 22 22 22 22 22 22 22 22 22 ...
 $ Group   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Handedness  : chr  left left left left ...
 $ Name: chr  2008_1 2008_1 2008_1 2008_1 ...
 $ SessionDate : chr  04-14-2008 04-14-2008
04-14-2008 04-14-2008 ...
 $ SessionTime : chr  11:07:31 11:07:31 11:07:31
11:07:31 ...
 $ Sex : chr  female female female female ...
 $ Block   : int  24 25 26 27 28 29 30 31 32 33 ...
 $ ABCorr  : int  0 1 2 3 4 1 2 3 4 5 ...
 $ ABRev   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ acc : int  0 1 1 1 1 1 1 1 1 1 ...
 $ ansr1   : int  1 1 2 2 1 2 1 1 1 2 ...
 $ BeginInstruction: int  NA NA NA NA NA NA NA NA NA NA ...
 $ BeginInstruction.Cycle  : int  NA NA NA NA NA NA NA NA NA NA ...
 $ BeginInstruction.Sample : int  NA NA NA NA NA NA NA NA NA NA ...
 $ BlockInst   : int  NA NA NA NA NA NA NA NA NA NA ...
 $ BlockInst.Cycle : int  NA NA NA NA NA NA NA NA NA NA ...
 $ BlockInst.Sample: int  NA NA NA NA NA NA NA NA NA NA ...
 $ incorrect   : int  2 2 1 1 2 1 2 2 2 1 ...
 $ LastCorr: int  0 0 0 0 0 4 4 4 4 4 ...
 $ LeftExample : chr  ...
 $ LeftStim: chr  f12.bmp f12.bmp f12.bmp f5.bmp ...
 $ Procedure   : chr  Trainb Trainb Trainb Trainb ...
 $ RepNum  : int  1 1 1 1 1 1 1 1 1 1 ...
 $ ReqCorr : int  11 11 11 11 11 11 11 11 11 11 ...
 $ RightExample: chr  ...
 $ RightStim   : chr  f5.bmp f5.bmp f5.bmp f12.bmp ...
 $ Running : chr  TrainRule TrainRule TrainRule
TrainRule ...
 $ Slide1_ACC  : int  0 1 0 1 1 1 1 1 1 0 ...
 $ Slide1_CRESP: int  1 1 2 2 1 2 1 1 1 2 ...
 $ Slide1_RESP : num  2 1 1 2 1 2 1 1 1 1 ...
 $ Slide1_RT   : int  1059 467 535 649 621 567 613 562 618 482 ...
 $ TrainInstructions   : int  NA NA NA NA NA NA NA NA NA NA ...
 $ TrainInstructions.Cycle : int  NA NA NA NA NA NA NA NA NA NA ...
 $ TrainInstructions.Sample: int  NA NA NA NA NA NA NA NA NA NA ...
 $ TrainListAB : int  1 1 2 1 1 1 1 1 1 2 ...
 $ TrainReversal   : int  NA NA NA NA NA NA NA NA NA NA ...
 $ TrainReversal.Cycle : int  NA NA NA NA NA NA NA NA NA NA ...
 $ TrainReversal.Sample: int  NA NA NA NA NA NA NA NA NA NA ...
 $ TrainRule   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ TrainRule.Cycle : int  31 32 33 34 35 36 37 38 39 40 ...
 $ TrainRule.Sample: int  21 22 23 24 25 26 27 28 29 30 ...
 $ TrainTestBlock  : int  1 1 1 1 1 1 1 1 1 1 ...
 $ TrialType   : chr  AB AB AB BA ...
 $ Valid   : int  1 1 0 1 1 1 1 1 1 0 ...

On Mon, Aug 8, 2011 at 12:35 PM, Edward Patzelt patze...@umn.edu wrote:

 Here's a thought, when I execute it on a data frame with only the subject
 and resp variables I have the same problem.  But not on the subset of data
 we've been working with.  The difference between the full vector and the
 subset vector is that the full vector contains NA's.  Only 35 out of 17,000
 trials, but could this screw up the function?

 unique(blah$resp)

 [1] 2 1 NA 4


 On Mon, Aug 8, 2011 at 11:46 AM, Joshua Wiley jwiley.ps...@gmail.comwrote:

 These are shots in the dark, but you could try: running it in a clean
 R session with only the necessary packages and data loaded; testing on
 subsets of your data; examing the classes of all your variables and
 make sure they are what is expexted; upgrading your version of R.

 I just retested this on:

 R version 2.12.1 (2010-12-16)
 Platform: x86_64-pc-mingw32/x64 (64-bit)

 locale:
 [1] LC_COLLATE=English_United States.1252
 [2] LC_CTYPE=English_United States.1252
 [3] LC_MONETARY=English_United States.1252
 [4] LC_NUMERIC=C
 [5] LC_TIME=English_United States.1252

 attached base packages:
 [1] grid  splines   stats graphics  grDevices utils datasets
 [8] methods   base

 other attached packages:
 [1] car_2.0-9   survival_2.36-5 nnet_7.3-1  MASS_7.3-11

 again without issue, so I am thinking there must be something
 particular perhaps to your session going on, but it is difficult to
 say.

 On Mon, Aug 8, 2011 at 9:35 AM, Edward Patzelt patze...@umn.edu wrote:
  H, I

[R] If find x, then y, else do nothing

2011-08-05 Thread Edward Patzelt
I want to write code that says If you find an element equal to 4 in this
vector for each person in the data set tested separately, then put in 1 for
2 and 2 for 4, else leave the variable as is

 u.ppl - (unique(init.dat1$grid))
  l.ppl - length(u.ppl)
for (i in 1:l.ppl)
{
  if (grep(4,init.dat1$Slide1_RESP)) {2 == 1, 4 == 2}; else
init.dat1$Slide1_RESP

}

-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] If find x, then y, else do nothing

2011-08-05 Thread Edward Patzelt
The problem is that we were using a task where some subjects responded with
1  2 and some responded with 2  4.  So there is overlap for 2 because
it means stimulus 1 for subject 1 and it means stimulus 2 for subject 2.

 subject

subject_1

1

subject_1

1

subject_1

1

subject_1

2

subject_1

2

subject_2

4

subject_2

2

subject_2

2

subject_2

4

subject_2

2

subject_2

2

subject_2

2

subject_2

4

On Fri, Aug 5, 2011 at 4:25 PM, Joshua Wiley jwiley.ps...@gmail.com wrote:

 Hi Edward,

 You can try something like:

 u.ppl - unique(init.dat1$grid)
 l.ppl - ifelse(grepl(4, init.dat1$Slide1_RESP), 2,
init.dat1$Slide1_RESP)

 Note that this is not exact as you have not provided a reproducible
 example.  I am not exactly sure how you are  putting 1 for 2 and 2 for
 4, if the value is equal to 4, but presumably it is clearer with data.
  In any event, look at ?ifelse it is something like a vectorized if
 statement and is, I believe, preferable to your use of a for loop.  I
 can probably give you a runnable solution if you can give the first
 few rows of the relevant data.

 Cheers,

 Josh

 On Fri, Aug 5, 2011 at 2:15 PM, Edward Patzelt patze...@umn.edu wrote:
  I want to write code that says If you find an element equal to 4 in this
  vector for each person in the data set tested separately, then put in 1
 for
  2 and 2 for 4, else leave the variable as is
 
   u.ppl - (unique(init.dat1$grid))
   l.ppl - length(u.ppl)
 for (i in 1:l.ppl)
 {
   if (grep(4,init.dat1$Slide1_RESP)) {2 == 1, 4 == 2}; else
  init.dat1$Slide1_RESP
 
 }
 
  --
  Edward H. Patzelt
  Research Assistant – TRiCAM Lab
  University of Minnesota – Psychology/Psychiatry
  VA Medical Center
  Office: S355 Elliot Hall - Twin Cities Campus
  Phone: 612-626-0072  Email: patze...@umn.edu
 
  Please consider the environment before printing this email
  www.psych.umn.edu/research/tricam
 
 [[alternative HTML version deleted]]
 
 
  __
  R-help@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.
 
 



 --
 Joshua Wiley
 Ph.D. Student, Health Psychology
 Programmer Analyst II, ATS Statistical Consulting Group
 University of California, Los Angeles
 https://joshuawiley.com/




-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


[R] Hidden Markov Models in R

2011-07-27 Thread Edward Patzelt
R Community -

I am attempting to fit a model as described in Hampton, Bossaerts, and
O'doherty (J. Neuroscience) 2006.  They use a bayesian hidden markov model
to model the Reversal Learning data.  I have tried using HMM and depmixS4
with no success.  My data is a Reversal Learning Task in which there are 3
sets of patterns over 3 blocks.  The participant receives incorrect or
correct feedback.  20% of the time they receive false feedback (they are
told incorrect when they were in fact correct).  Once the person achieves
the criterion of 9/10 correct responses the contigencies reverse.  I am
confused on how to set up my states, symbols, starting probabilities,
transition probabilities, and emission probabilities in R.  This is what I
have so far.

hmm - initHMM(c(stay, switch), c(correct, incorrect), c(.5, .5),
matrix(c(.9, .1, .1, .9),2), matrix(c(.2, .8, .8, .2), 2))

dat$test - ifelse(dat$Slide1_ACC == 0, incorrect, correct)

viterbi(hmm, dat$test)


The sequence of observations I run through the model is the feedback the
participant receives.  Any help would be greatly appreciated.  I think what
I want to do is run the model on each participant to generate the most
probable path and then compare that to the actual path to see if they match
up.

Best,



-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


[R] Loop through each subject

2011-06-28 Thread Edward Patzelt
R help -

I am attempting to write a script that has multiple subjects in 1 data file.
 Each subject has multiple rows with columns as variables.  Here is my code,
I am having problem executing it on each unique subject id (dat$Subject).

   getwd()

setwd(/Users/edwardpatzelt/Desktop/Neuroimaging/MERIT/SRRT/merge)


dat - read.table(test2.txt, header = TRUE, na.strings = NA,
stringsAsFactors = FALSE, sep = \t)


for(i in 1:length(dat))

 {

 for (i in 1:)dat[(unique(dat$Subject)),)]

 {

colg - dat[grep(Green, dat$CueProbe),]

   colg - data.frame(colg$SRRTCue.OnsetTime/1000, (colg$SRRTFix2.OnsetTime-
colg$SRRTCue.OnsetTime)/1000, (ifelse((colg$SRRTProbe.ACC == 1 | colg$Probe==
+), 1, 0)))

   colr - dat[grep(Red, dat$CueProbe),]

   colr - data.frame(colr$SRRTCue.OnsetTime/1000, (colr$SRRTFix2.OnsetTime-
colr$SRRTCue.OnsetTime)/1000, (ifelse((colr$SRRTProbe.ACC == 1 | colr$Probe==
+), 1, 0)))

 write.table(colg, file  = paste(dat$Subject[[1]], sep = \t, append =
green.txt), col.names = FALSE, row.names = FALSE)

  write.table(colr, file  = paste(dat$Subject[[1]], sep = \t, append =
red.txt), col.names = FALSE, row.names = FALSE)

 }

  }






-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Loop through each subject

2011-06-28 Thread Edward Patzelt
Huh, not sure I understand your feedback.  There is only 1 file that is 1
large dataframe.  I want to execute the commands on each subject in the
dataframe and ouput their respective files with the subject # and file type
appended (10_green.txt).

How would I do this loop index?
- Show quoted text -

On Tue, Jun 28, 2011 at 1:27 PM, David Winsemius dwinsem...@comcast.netwrote:


 On Jun 28, 2011, at 1:44 PM, Edward Patzelt wrote:

  R help -

 I am attempting to write a script that has multiple subjects in 1 data
 file.
 Each subject has multiple rows with columns as variables.  Here is my
 code,
 I am having problem executing it on each unique subject id (dat$Subject).


 One problem that I see is that you are calling all of your files the same
 thing (i.e. overwriting earlier results. Why aren't you using the loop index
 in the naming process?

 (And aeppnd is a logical argument in write.table.)

 ?write.table

 --
 David.


  getwd()

 setwd(/Users/edwardpatzelt/**Desktop/Neuroimaging/MERIT/**SRRT/merge)


 dat - read.table(test2.txt, header = TRUE, na.strings = NA,
 stringsAsFactors = FALSE, sep = \t)


 for(i in 1:length(dat))

 {

 for (i in 1:)dat[(unique(dat$Subject)),)**]

 {

   colg - dat[grep(Green, dat$CueProbe),]

  colg - data.frame(colg$SRRTCue.**OnsetTime/1000,
 (colg$SRRTFix2.OnsetTime-
 colg$SRRTCue.OnsetTime)/1000, (ifelse((colg$SRRTProbe.ACC == 1 |
 colg$Probe==
 +), 1, 0)))

  colr - dat[grep(Red, dat$CueProbe),]

  colr - data.frame(colr$SRRTCue.**OnsetTime/1000,
 (colr$SRRTFix2.OnsetTime-
 colr$SRRTCue.OnsetTime)/1000, (ifelse((colr$SRRTProbe.ACC == 1 |
 colr$Probe==
 +), 1, 0)))

write.table(colg, file  = paste(dat$Subject[[1]], sep = \t, append =
 green.txt), col.names = FALSE, row.names = FALSE)

  write.table(colr, file  = paste(dat$Subject[[1]], sep = \t, append =
 red.txt), col.names = FALSE, row.names = FALSE)

}

  }






 --
 Edward H. Patzelt
 Research Assistant – TRiCAM Lab
 University of Minnesota – Psychology/Psychiatry
 VA Medical Center
 Office: S355 Elliot Hall - Twin Cities Campus
 Phone: 612-626-0072  Email: patze...@umn.edu

 Please consider the environment before printing this email
 www.psych.umn.edu/research/**tricamhttp://www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

 __**
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


 David Winsemius, MD
 West Hartford, CT




-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: S355 Elliot Hall - Twin Cities Campus
Phone: 612-626-0072  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.


[R] nlme Output

2010-09-06 Thread Edward Patzelt
Everyone -

What do the NaN's mean here?  Is this analysis a problem?


Linear mixed-effects model fit by maximum likelihood
 Data: tmp.dat
   AIC  BIClogLik
  1611.251 1638.363 -797.6253

Random effects:
 Formula: ~1 | group_id
 (Intercept) Residual
StdDev: 0.0003077668 9.236715

Fixed effects: AvgTrials ~ time + factor(group_id) + time *
factor(group_id)
   Value Std.Error  DF   t-value p-value
(Intercept)18.159722  3.576664 213  5.077279  0.
time4.192708  1.655674 213  2.532327  0.0121
factor(group_id)2  -6.929563  5.235700   0 -1.323522 NaN
factor(group_id)3  -1.654554  4.189575   0 -0.394922 NaN
time:factor(group_id)2  1.729911  2.423658 213  0.713760  0.4762
time:factor(group_id)3 -2.555111  1.939396 213 -1.317478  0.1891
 Correlation:
   (Intr) time   fc(_)2 fc(_)3 t:(_)2
time   -0.926
factor(group_id)2  -0.683  0.632
factor(group_id)3  -0.854  0.790  0.583
time:factor(group_id)2  0.632 -0.683 -0.926 -0.540
time:factor(group_id)3  0.790 -0.854 -0.540 -0.926  0.583

Standardized Within-Group Residuals:
   Min Q1Med Q3Max
-1.8842754 -0.6979785 -0.3370998  0.5666704  3.0943948

Number of Observations: 219
Number of Groups: 3
Warning message:
In pt(q, df, lower.tail, log.p) : NaNs produced

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] nlme Output

2010-09-06 Thread Edward Patzelt
The design is a repeated measures with 3 instances.  There are 3 groups:
 Controls, Heavy Cocaine Users, Light Cocaine Users.

I reshaped the data so that there was one variable for the 3 instances
called AvgTrials.  Time is the indicator of each instance.

Here is the model call:

mod5 - lme(AvgTrials ~ time + factor(group_id) + time*factor(group_id),
random = ~ 1 | group_id, data = tmp.dat, method = ML, na.action = na.omit)

What else would you need here?

-Edward


On Mon, Sep 6, 2010 at 8:52 AM, ONKELINX, Thierry
thierry.onkel...@inbo.bewrote:

 Dear Edward,

 You have no degrees of freedom left to estimate those p-values. Your
 design does not allows for the model your implemented. We need a brief
 summary of your design in order to help you further.

 HTH,

 Thierry

 
 
 ir. Thierry Onkelinx
 Instituut voor natuur- en bosonderzoek
 team Biometrie  Kwaliteitszorg
 Gaverstraat 4
 9500 Geraardsbergen
 Belgium

 Research Institute for Nature and Forest
 team Biometrics  Quality Assurance
 Gaverstraat 4
 9500 Geraardsbergen
 Belgium

 tel. + 32 54/436 185
 thierry.onkel...@inbo.be
 www.inbo.be

 To call in the statistician after the experiment is done may be no more
 than asking him to perform a post-mortem examination: he may be able to
 say what the experiment died of.
 ~ Sir Ronald Aylmer Fisher

 The plural of anecdote is not data.
 ~ Roger Brinner

 The combination of some data and an aching desire for an answer does not
 ensure that a reasonable answer can be extracted from a given body of
 data.
 ~ John Tukey


  -Oorspronkelijk bericht-
  Van: r-help-boun...@r-project.org
  [mailto:r-help-boun...@r-project.org] Namens Edward Patzelt
  Verzonden: maandag 6 september 2010 15:43
  Aan: r-help@r-project.org
  Onderwerp: [R] nlme Output
 
  Everyone -
 
  What do the NaN's mean here?  Is this analysis a problem?
 
 
  Linear mixed-effects model fit by maximum likelihood
   Data: tmp.dat
 AIC  BIClogLik
1611.251 1638.363 -797.6253
 
  Random effects:
   Formula: ~1 | group_id
   (Intercept) Residual
  StdDev: 0.0003077668 9.236715
 
  Fixed effects: AvgTrials ~ time + factor(group_id) + time *
  factor(group_id)
 Value Std.Error  DF   t-value p-value
  (Intercept)18.159722  3.576664 213  5.077279  0.
  time4.192708  1.655674 213  2.532327  0.0121
  factor(group_id)2  -6.929563  5.235700   0 -1.323522 NaN
  factor(group_id)3  -1.654554  4.189575   0 -0.394922 NaN
  time:factor(group_id)2  1.729911  2.423658 213  0.713760  0.4762
  time:factor(group_id)3 -2.555111  1.939396 213 -1.317478  0.1891
   Correlation:
 (Intr) time   fc(_)2 fc(_)3 t:(_)2
  time   -0.926
  factor(group_id)2  -0.683  0.632
  factor(group_id)3  -0.854  0.790  0.583
  time:factor(group_id)2  0.632 -0.683 -0.926 -0.540
  time:factor(group_id)3  0.790 -0.854 -0.540 -0.926  0.583
 
  Standardized Within-Group Residuals:
 Min Q1Med Q3Max
  -1.8842754 -0.6979785 -0.3370998  0.5666704  3.0943948
 
  Number of Observations: 219
  Number of Groups: 3
  Warning message:
  In pt(q, df, lower.tail, log.p) : NaNs produced
 
[[alternative HTML version deleted]]
 
  __
  R-help@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.
 

 Druk dit bericht a.u.b. niet onnodig af.
 Please do not print this message unnecessarily.

 Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver
 weer
 en binden het INBO onder geen enkel beding, zolang dit bericht niet
 bevestigd is
 door een geldig ondertekend document. The views expressed in  this message
 and any annex are purely those of the writer and may not be regarded as
 stating
 an official position of INBO, as long as the message is not confirmed by a
 duly
 signed document.




-- 
Edward H. Patzelt
Research Assistant – TRiCAM Lab
University of Minnesota – Psychology/Psychiatry
VA Medical Center
Office: N437 Elliot Hall - Twin Cities Campus
Phone: 612-624-3892  Email: patze...@umn.edu

Please consider the environment before printing this email
www.psych.umn.edu/research/tricam

[[alternative HTML version deleted]]

__
R-help@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.