Calculate post error slowing in R - r

For my research, I would like to calculate the post-error slowing in the stop signal task to find out whether people become slower after they failed to inhibit their response. Here is some data and I would like to do the following:
For each subject determine first if it was a stop-trial (signal = 1)
For each stop-trial, determine if it is correct (signal = 1 & correct = 2) and then determine whether the next trial (thus the trial directly after the stop-trial) is a go-trial (signal = 0)
Then calculate the average reaction time for all these go-trials that directly follow a stop trial when the response is correct (signal = 0 & correct = 2).
For each incorrect stop trial (signal = 1 & correct = 0) determine whether the next trial (thus the trial directly after the stop-trial) is a go-trial (signal = 0)
Then calculate the average reaction time for all these go-trials that directly follow a stop-trial when the response is correct (correct = 2).
Then calculate the difference between the RTs calculated in step 2 and 3 (= post-error slowing).
I'm not that experienced in R to achieve this. I hope someone can help me with this script.
subject trial signal correct RT
1 1 0 2 755
1 2 0 2 543
1 3 1 0 616
1 4 0 2 804
1 5 0 2 594
1 6 0 2 705
1 7 1 2 0
1 8 1 2 0
1 9 0 2 555
1 10 1 0 604
1 11 0 2 824
1 12 0 2 647
1 13 0 2 625
1 14 0 2 657
1 15 1 0 578
1 16 0 2 810
1 17 1 2 0
1 18 0 2 646
1 19 0 2 574
1 20 0 2 748
1 21 0 0 856
1 22 0 2 679
1 23 0 2 738
1 24 0 2 620
1 25 0 2 715
1 26 1 2 0
1 27 0 2 675
1 28 0 2 560
1 29 1 0 584
1 30 0 2 564
1 31 0 2 994
1 32 1 2 0
1 33 0 2 715
1 34 0 2 644
1 35 0 2 545
1 36 0 2 528
1 37 1 2 0
1 38 0 2 636
1 39 0 2 684
1 40 1 2 0
1 41 0 2 653
1 42 0 2 766
1 43 0 2 747
1 44 0 2 821
1 45 0 2 612
1 46 0 2 624
1 47 0 2 665
1 48 1 2 0
1 49 0 2 594
1 50 0 2 665
1 51 1 0 658
1 52 0 2 800
1 53 1 2 0
1 54 1 0 738
1 55 0 2 831
1 56 0 2 815
1 57 0 2 776
1 58 0 2 710
1 59 0 2 842
1 60 1 0 516
1 61 0 2 758
1 62 1 2 0
1 63 0 2 628
1 64 0 2 713
1 65 0 2 835
1 66 1 0 791
1 67 0 2 871
1 68 0 2 816
1 69 0 2 769
1 70 0 2 930
1 71 0 2 676
1 72 0 2 868
2 1 0 2 697
2 2 0 2 689
2 3 0 2 584
2 4 1 0 788
2 5 0 2 448
2 6 0 2 564
2 7 0 2 587
2 8 1 0 553
2 9 0 2 706
2 10 0 2 442
2 11 1 0 245
2 12 0 2 601
2 13 0 2 774
2 14 1 0 579
2 15 0 2 652
2 16 0 2 556
2 17 0 2 963
2 18 0 2 725
2 19 0 2 751
2 20 0 2 709
2 21 0 2 741
2 22 1 0 613
2 23 0 2 781
2 24 1 2 0
2 25 0 2 634
2 26 1 2 0
2 27 0 2 487
2 28 1 2 0
2 29 0 2 692
2 30 0 2 745
2 31 1 2 0
2 32 0 2 610
2 33 0 2 836
2 34 1 0 710
2 35 0 2 757
2 36 0 2 781
2 37 0 2 1029
2 38 0 2 832
2 39 1 0 626
2 40 1 2 0
2 41 0 2 844
2 42 0 2 837
2 43 0 2 792
2 44 0 2 789
2 45 0 2 783
2 46 0 0 0
2 47 0 0 468
2 48 0 2 686

This may be too late to be useful but here's my solution: (i.e. I first split the data frame by subject, and then apply the same algorithm to each subject; the result is:
# 1 2
# -74.60317 23.39286
X <- read.table(
text=" subject trial signal correct RT
1 1 0 2 755
1 2 0 2 543
1 3 1 0 616
1 4 0 2 804
1 5 0 2 594
1 6 0 2 705
1 7 1 2 0
1 8 1 2 0
1 9 0 2 555
1 10 1 0 604
1 11 0 2 824
1 12 0 2 647
1 13 0 2 625
1 14 0 2 657
1 15 1 0 578
1 16 0 2 810
1 17 1 2 0
1 18 0 2 646
1 19 0 2 574
1 20 0 2 748
1 21 0 0 856
1 22 0 2 679
1 23 0 2 738
1 24 0 2 620
1 25 0 2 715
1 26 1 2 0
1 27 0 2 675
1 28 0 2 560
1 29 1 0 584
1 30 0 2 564
1 31 0 2 994
1 32 1 2 0
1 33 0 2 715
1 34 0 2 644
1 35 0 2 545
1 36 0 2 528
1 37 1 2 0
1 38 0 2 636
1 39 0 2 684
1 40 1 2 0
1 41 0 2 653
1 42 0 2 766
1 43 0 2 747
1 44 0 2 821
1 45 0 2 612
1 46 0 2 624
1 47 0 2 665
1 48 1 2 0
1 49 0 2 594
1 50 0 2 665
1 51 1 0 658
1 52 0 2 800
1 53 1 2 0
1 54 1 0 738
1 55 0 2 831
1 56 0 2 815
1 57 0 2 776
1 58 0 2 710
1 59 0 2 842
1 60 1 0 516
1 61 0 2 758
1 62 1 2 0
1 63 0 2 628
1 64 0 2 713
1 65 0 2 835
1 66 1 0 791
1 67 0 2 871
1 68 0 2 816
1 69 0 2 769
1 70 0 2 930
1 71 0 2 676
1 72 0 2 868
2 1 0 2 697
2 2 0 2 689
2 3 0 2 584
2 4 1 0 788
2 5 0 2 448
2 6 0 2 564
2 7 0 2 587
2 8 1 0 553
2 9 0 2 706
2 10 0 2 442
2 11 1 0 245
2 12 0 2 601
2 13 0 2 774
2 14 1 0 579
2 15 0 2 652
2 16 0 2 556
2 17 0 2 963
2 18 0 2 725
2 19 0 2 751
2 20 0 2 709
2 21 0 2 741
2 22 1 0 613
2 23 0 2 781
2 24 1 2 0
2 25 0 2 634
2 26 1 2 0
2 27 0 2 487
2 28 1 2 0
2 29 0 2 692
2 30 0 2 745
2 31 1 2 0
2 32 0 2 610
2 33 0 2 836
2 34 1 0 710
2 35 0 2 757
2 36 0 2 781
2 37 0 2 1029
2 38 0 2 832
2 39 1 0 626
2 40 1 2 0
2 41 0 2 844
2 42 0 2 837
2 43 0 2 792
2 44 0 2 789
2 45 0 2 783
2 46 0 0 0
2 47 0 0 468
2 48 0 2 686", header=TRUE)
sapply(split(X, X["subject"]), function(D){
PCRT <- with(D, RT[which(c(signal[-1],NA)==1 & c(correct[-1], NA)==2 & signal==0) ])
PERT <- with(D, RT[which(c(signal[-1],NA)==1 & c(correct[-1], NA)==0 & signal==0) ])
mean(PERT) - mean(PCRT)
})
This is ok if you can be sure that every respondent has at least 1 correct and 1 incorrect "stop" trial followed by a "go" trial. A more general case would be (giving NA if they are either always correct or always mistaken):
sapply(split(X, X["subject"]), function(D){
PCRT <- with(D, RT[which(c(signal[-1],NA)==1 & c(correct[-1], NA)==2 & signal==0) ])
PERT <- with(D, RT[which(c(signal[-1],NA)==1 & c(correct[-1], NA)==0 & signal==0) ])
if(length(PCRT)>0 & length(PERT)>0) mean(PERT) - mean(PCRT) else NA
})

Does that help you? A little bit redundant maybe, but I tried to follow your steps as best as possible (not sure whether I mixed something up, please check for yourself looking at the table). The idea is to put the data in a csv file first and treat it as a data frame. Find the csv raw file here: http://pastebin.com/X5b2ysmQ
data <- read.csv("datatable.csv",header=T)
data[,"condition1"] <- data[,"signal"] == 1
data[,"condition2"] <- data[,"condition1"] & data[,"correct"] == 2
data[,"RT1"] <- NA
for(i in which(data[,"condition2"])){
if( nrow(data)>i && !data[i+1,"condition1"] && data[i+1,"correct"] == 2 )
# next is a go trial
data[i+1,"RT1"] <- data[i+1,"RT"]
}
averageRT1 <- mean( data[ !is.na(data[,"RT1"]) ,"RT1"] )
data[,"RT2"] <- NA
for(i in which(data[,"condition1"] & data[,"correct"] == 0)){
if( nrow(data)>i && !data[i+1,"condition1"] && data[i+1,"correct"] == 2 )
# next is a go trial
data[i+1,"RT2"] <- data[i+1,"RT"]
}
averageRT2 <- mean( data[ !is.na(data[,"RT2"]) ,"RT2"] )
postErrorSlowing <- abs(averageRT2-averageRT1)

#Nilsole I just tried it and it is almost perfect. How could the code be improved that for each subject the postErrorSlowing is calculated and placed in a dataframe? Thus that a new data frame is created which consists of subject number (1,2,3 etc.) and the postErrorSlowing variable? Something like this (postErrorSlowing are made up numbers)
subject postErrorSlowing
1 50
2 75
....

Related

Using lme to generate an MMRM model in R - incompatible formulas for groups in 'random' and 'correlation'

I'm trying to fit an MMRM to a dataset, there are specific covariates that have been pre-specified as random.
Previously, I have experience using gls() and simply accounting for the random effects in an unstructured correlation matrix via the below as an example:
nlme::corSymm(form = ~time | pt_num)
Since gls() doesn't specify random effects I'm trying to use lme, but I'm getting the error:
Error in lme.formula(la_mm2 ~ factor(arm) + factor(partI_bool) + la_base_mm2, :
incompatible formulas for groups in 'random' and 'correlation'
I've pasted some of the dummy data below as well as the model that I'm using, any clarity on what is causing the issue is highly appreciated!
#Model
gather1_mmrm <- lme(la_mm2 ~ factor(arm) + factor(partI_bool) + la_base_mm2,
data = gather1,
method = "REML",
random = ~t_factor + t_factor*factor(arm) + t_factor*factor(partI_bool) + t_factor*la_base_mm2 | pt_id,
na.action = na.omit,
correlation = nlme::corSymm(form= ~ t_factor | pt_num),
weights = nlme::varIdent(form= ~1|t_factor))
run pt_id arm partI_bool la_base_mm2 la_base_mm t_factor t_months la_mm2 la_mm ch_la_mm2 ch_la_mm
1 4 SHAM 1 4.302596153 2.074270029 1 0 4.302596153 2.074270029 0 0
1 4 SHAM 1 4.302596153 2.074270029 2 6 NA NA NA NA
1 4 SHAM 1 4.302596153 2.074270029 3 12 NA NA NA NA
1 12 SHAM 1 14.34691312 3.787731923 1 0 14.34691312 3.787731923 0 0
1 12 SHAM 1 14.34691312 3.787731923 2 6 15.64964748 3.955963533 1.302734357 1.302734357
1 12 SHAM 1 14.34691312 3.787731923 3 12 17.78969962 4.217783733 3.442786498 3.442786498
1 13 SHAM 1 5.356110596 2.314327245 1 0 5.356110596 2.314327245 0 0
1 13 SHAM 1 5.356110596 2.314327245 2 6 7.10663327 2.665826939 1.750522674 1.750522674
1 13 SHAM 1 5.356110596 2.314327245 3 12 9.041039758 3.00683218 3.684929162 3.684929162
1 14 SHAM 1 11.25759063 3.35523332 1 0 11.25759063 3.35523332 0 0
1 14 SHAM 1 11.25759063 3.35523332 2 6 13.61993655 3.690519821 2.362345918 2.362345918
1 14 SHAM 1 11.25759063 3.35523332 3 12 16.42931581 4.053309242 5.171725175 5.171725175
1 19 SHAM 1 2.758491755 1.660870782 1 0 2.758491755 1.660870782 0 0
1 19 SHAM 1 2.758491755 1.660870782 2 6 3.739763552 1.933846828 0.981271798 0.981271798
1 19 SHAM 1 2.758491755 1.660870782 3 12 4.772096698 2.18451292 2.013604943 2.013604943
1 35 SHAM 0 3.268341978 1.80785563 1 0 3.268341978 1.80785563 0 0
1 35 SHAM 0 3.268341978 1.80785563 2 6 3.617463276 1.901963006 0.349121298 0.349121298
1 35 SHAM 0 3.268341978 1.80785563 3 12 4.042171268 2.010515175 0.77382929 0.77382929
1 39 SHAM 0 5.806441903 2.409655972 1 0 5.806441903 2.409655972 0 0
1 39 SHAM 0 5.806441903 2.409655972 2 6 6.668033763 2.582253621 0.86159186 0.86159186
1 39 SHAM 0 5.806441903 2.409655972 3 12 6.657201504 2.580155326 0.850759601 0.850759601
1 51 SHAM 0 6.726560406 2.593561336 1 0 6.726560406 2.593561336 0 0
1 51 SHAM 0 6.726560406 2.593561336 2 6 6.752801108 2.598615229 0.026240702 0.026240702
1 51 SHAM 0 6.726560406 2.593561336 3 12 7.14950777 2.673856348 0.422947364 0.422947364
1 55 SHAM 0 5.281358119 2.298120562 1 0 5.281358119 2.298120562 0 0
1 55 SHAM 0 5.281358119 2.298120562 2 6 3.504983367 1.872160081 -1.776374752 -1.776374752
1 55 SHAM 0 5.281358119 2.298120562 3 12 1.453285056 1.205522732 -3.828073063 -3.828073063
1 103 SHAM 0 14.14175968 3.760553108 1 0 14.14175968 3.760553108 0 0
1 103 SHAM 0 14.14175968 3.760553108 2 6 15.83732887 3.979614161 1.695569189 1.695569189
1 103 SHAM 0 14.14175968 3.760553108 3 12 17.85259321 4.225232918 3.710833529 3.710833529
1 105 SHAM 0 8.118501066 2.849298346 1 0 8.118501066 2.849298346 0 0
1 105 SHAM 0 8.118501066 2.849298346 2 6 NA NA NA NA
1 105 SHAM 0 8.118501066 2.849298346 3 12 NA NA NA NA
1 107 SHAM 0 16.25989413 4.032355903 1 0 16.25989413 4.032355903 0 0
1 107 SHAM 0 16.25989413 4.032355903 2 6 NA NA NA NA
1 107 SHAM 0 16.25989413 4.032355903 3 12 NA NA NA NA
1 110 SHAM 0 3.108610876 1.763125315 1 0 3.108610876 1.763125315 0 0
1 110 SHAM 0 3.108610876 1.763125315 2 6 NA NA NA NA
1 110 SHAM 0 3.108610876 1.763125315 3 12 NA NA NA NA
1 122 2mg 1 14.23013034 3.772284499 1 0 14.23013034 3.772284499 0 0
1 122 2mg 1 14.23013034 3.772284499 2 6 16.31252812 4.038877086 2.082397778 2.082397778
1 122 2mg 1 14.23013034 3.772284499 3 12 17.26079811 4.154611667 3.030667766 3.030667766
1 129 2mg 1 5.839250811 2.416454181 1 0 5.839250811 2.416454181 0 0
1 129 2mg 1 5.839250811 2.416454181 2 6 7.464223376 2.732073091 1.624972565 1.624972565
1 129 2mg 1 5.839250811 2.416454181 3 12 9.640338241 3.104889409 3.80108743 3.80108743
1 133 2mg 1 4.413316452 2.100789483 1 0 4.413316452 2.100789483 0 0
1 133 2mg 1 4.413316452 2.100789483 2 6 5.064445674 2.25043233 0.651129222 0.651129222
1 133 2mg 1 4.413316452 2.100789483 3 12 NA NA NA NA
1 139 2mg 0 11.85320922 3.442848998 1 0 11.85320922 3.442848998 0 0
1 139 2mg 0 11.85320922 3.442848998 2 6 13.7069869 3.702294815 1.853777679 1.853777679
1 139 2mg 0 11.85320922 3.442848998 3 12 14.36968627 3.790736904 2.516477054 2.516477054
1 155 2mg 0 9.226600647 3.037531999 1 0 9.226600647 3.037531999 0 0
1 155 2mg 0 9.226600647 3.037531999 2 6 8.73090644 2.954810728 -0.495694207 -0.495694207
1 155 2mg 0 9.226600647 3.037531999 3 12 9.042412659 3.007060468 -0.184187988 -0.184187988
1 160 2mg 0 12.06173395 3.473000713 1 0 12.06173395 3.473000713 0 0
1 160 2mg 0 12.06173395 3.473000713 2 6 12.47205033 3.531579013 0.410316377 0.410316377
1 160 2mg 0 12.06173395 3.473000713 3 12 NA NA NA NA
1 161 2mg 0 6.051340791 2.459947315 1 0 6.051340791 2.459947315 0 0
1 161 2mg 0 6.051340791 2.459947315 2 6 NA NA NA NA
1 161 2mg 0 6.051340791 2.459947315 3 12 5.371776645 2.317709353 -0.679564146 -0.679564146
1 168 2mg 0 13.47495294 3.670824559 1 0 13.47495294 3.670824559 0 0
1 168 2mg 0 13.47495294 3.670824559 2 6 15.60077961 3.949782224 2.125826674 2.125826674
1 168 2mg 0 13.47495294 3.670824559 3 12 17.56628938 4.19121574 4.091336443 4.091336443
1 184 4mg 0 11.41583874 3.378733304 1 0 11.41583874 3.378733304 0 0
1 184 4mg 0 11.41583874 3.378733304 2 6 13.27277419 3.643181877 1.856935451 1.856935451
1 184 4mg 0 11.41583874 3.378733304 3 12 15.59128977 3.948580729 4.175451032 4.175451032
1 189 4mg 0 5.743634485 2.396588092 1 0 5.743634485 2.396588092 0 0
1 189 4mg 0 5.743634485 2.396588092 2 6 5.228250162 2.286536718 -0.515384323 -0.515384323
1 189 4mg 0 5.743634485 2.396588092 3 12 4.484122564 2.117574689 -1.259511921 -1.259511921
1 197 4mg 0 9.077401292 3.012872598 1 0 9.077401292 3.012872598 0 0
1 197 4mg 0 9.077401292 3.012872598 2 6 NA NA NA NA
1 197 4mg 0 9.077401292 3.012872598 3 12 NA NA NA NA
1 214 4mg 0 11.90323176 3.450106051 1 0 11.90323176 3.450106051 0 0
1 214 4mg 0 11.90323176 3.450106051 2 6 13.60561692 3.688579255 1.70238516 1.70238516
1 214 4mg 0 11.90323176 3.450106051 3 12 15.36408886 3.9197052 3.460857095 3.460857095
1 218 4mg 0 10.8365073 3.291885068 1 0 10.8365073 3.291885068 0 0
1 218 4mg 0 10.8365073 3.291885068 2 6 12.0130533 3.465985184 1.176545997 1.176545997
1 218 4mg 0 10.8365073 3.291885068 3 12 13.63051081 3.691952168 2.794003509 2.794003509
1 231 4mg 0 10.32451428 3.213178221 1 0 10.32451428 3.213178221 0 0
1 231 4mg 0 10.32451428 3.213178221 2 6 11.11654749 3.33414869 0.792033208 0.792033208
1 231 4mg 0 10.32451428 3.213178221 3 12 11.13792298 3.33735269 0.813408696 0.813408696
1 235 4mg 0 6.26445047 2.502888425 1 0 6.26445047 2.502888425 0 0
1 235 4mg 0 6.26445047 2.502888425 2 6 NA NA NA NA
1 235 4mg 0 6.26445047 2.502888425 3 12 7.748527082 2.783617625 1.484076612 1.484076612
1 237 4mg 0 5.393093093 2.322303402 1 0 5.393093093 2.322303402 0 0
1 237 4mg 0 5.393093093 2.322303402 2 6 6.382859882 2.526432244 0.989766789 0.989766789
1 237 4mg 0 5.393093093 2.322303402 3 12 6.65907103 2.58051759 1.265977937 1.265977937
1 250 4mg 0 3.578464204 1.891682903 1 0 3.578464204 1.891682903 0 0
1 250 4mg 0 3.578464204 1.891682903 2 6 3.922992029 1.980654445 0.344527825 0.344527825
1 250 4mg 0 3.578464204 1.891682903 3 12 4.405971911 2.099040712 0.827507707 0.827507707

Creating a new column in R

I have a data.frame like the following:
regions admit men_age group
1 1234 34 2
2 3416 51 1
3 2463 26 3
4 1762 29 2
5 2784 31 4
6 999 42 1
7 2111 23 2
8 1665 36 3
9 2341 21 4
10 1723 33 1
I would like to create new columns using admit and group as follows:
regions admit men_age group admit1 admit2 admit3 admit4
1 1234 34 2 0 1234 0 0
2 3416 51 1 3416 0 0 0
3 2463 26 3 0 0 2463 0
4 1762 29 2 0 1762 0 0
5 2784 31 4 0 0 0 2784
6 999 42 1 999 0 0 0
7 2111 23 2 0 2111 0 0
8 1665 36 3 0 0 1665 0
9 2341 21 4 0 0 0 2341
10 1723 33 1 1723 0 0 0
In fact, what I want to do is to create four new admit columns according to group column as follows: in admit 1 column, the value for rows where group is 1, put the corresponding admit number, other wise put zero. In admit 2 column, the values for rows where group is 2, put the corresponding admit number, otherwise put zero ans this applies for two other column as well.
I tried a couple of ways to solve it, but failed.
May please someone help me to solve this?
A solution using tidyverse. We can create the columns and then spread them with fill = 0.
library(tidyverse)
dat2 <- dat %>%
mutate(group2 = str_c("admit", group), admit2 = admit) %>%
spread(group2, admit2, fill = 0)
dat2
# regions admit men_age group admit1 admit2 admit3 admit4
# 1 1 1234 34 2 0 1234 0 0
# 2 2 3416 51 1 3416 0 0 0
# 3 3 2463 26 3 0 0 2463 0
# 4 4 1762 29 2 0 1762 0 0
# 5 5 2784 31 4 0 0 0 2784
# 6 6 999 42 1 999 0 0 0
# 7 7 2111 23 2 0 2111 0 0
# 8 8 1665 36 3 0 0 1665 0
# 9 9 2341 21 4 0 0 0 2341
# 10 10 1723 33 1 1723 0 0 0
DATA
dat <- read.table(text = "regions admit men_age group
1 1234 34 2
2 3416 51 1
3 2463 26 3
4 1762 29 2
5 2784 31 4
6 999 42 1
7 2111 23 2
8 1665 36 3
9 2341 21 4
10 1723 33 1",
header = TRUE)
A Base R solution would be using ifelse(). Supposed you data.frame is x, you could do this:
# create the columns with the selected values
for( i in 1:4 ) x[ i + 4 ] <- ifelse( x$group == i, x$admit, 0 )
# rename the columns to your liking
colnames( x )[ 5:8 ] <- c( "admit1", "admit2", "admit3", "admit4" )
This gives you
> x
regions admit men_age group admit1 admit2 admit3 admit4
1 1 1234 34 2 0 1234 0 0
2 2 3416 51 1 3416 0 0 0
3 3 2463 26 3 0 0 2463 0
4 4 1762 29 2 0 1762 0 0
5 5 2784 31 4 0 0 0 2784
6 6 999 42 1 999 0 0 0
7 7 2111 23 2 0 2111 0 0
8 8 1665 36 3 0 0 1665 0
9 9 2341 21 4 0 0 0 2341
10 10 1723 33 1 1723 0 0 0
If you don't like the explicit naming, you could do it in the for() loop already:
for( i in 1:4 )
{
adm <- paste ( "admit", i, sep = "" )
x[ adm ] <- ifelse( x$group == i, x$admit, 0 )
}

How to save for loop results in data frame using cbind

I have a data frame dfSub with a number of parameters inside. This is hourly based data for energy use. I need to sort data by each hour, e.g. for each hour get all values of energy from data frame. As a result I expect to have data frame with 24 columns for each hour, rows are filled with energy values.
The hour is specified as 1:24 and in data frame is linked as dfSub$hr.
The heat is dfSub$heat
I constructed a for-loop and tried to save with cbind, but it does not work, error message is about different size of rows and columns.
I print results and see them on screen, but cant save as d(dataframe)
here is the code:
d = NULL
for (i in 1:24) {
subh= subset(dfSub$heat, dfSub$hr == i)
print(subh)
d = cbind(d, as.data.frame(subh))
}
append function is not applicable, since I dont know the expected length of heat value for each hour.
Any help is appreciated.
Part of dfSub
hr wk month dyid wend t heat
1 2 1 1 0 -9.00 81
2 2 1 1 0 -8.30 61
3 2 1 1 0 -7.80 53
4 2 1 1 0 -7.00 51
5 2 1 1 0 -7.00 30
6 2 1 1 0 -6.90 31
7 2 1 1 0 -7.10 51
8 2 1 1 0 -6.50 90
9 2 1 1 0 -8.90 114
10 2 1 1 0 -9.90 110
11 2 1 1 0 -11.70 126
12 2 1 1 0 -9.70 113
13 2 1 1 0 -11.60 104
14 2 1 1 0 -10.00 107
15 2 1 1 0 -10.20 117
16 2 1 1 0 -9.00 90
17 2 1 1 0 -8.00 114
18 2 1 1 0 -7.80 83
19 2 1 1 0 -8.10 82
20 2 1 1 0 -8.20 61
21 2 1 1 0 -8.80 34
22 2 1 1 0 -9.10 52
23 2 1 1 0 -10.10 41
24 2 1 1 0 -8.80 52
1 2 1 2 0 -8.70 44
2 2 1 2 0 -8.40 50
3 2 1 2 0 -8.10 33
4 2 1 2 0 -7.70 41
5 2 1 2 0 -7.80 33
6 2 1 2 0 -7.50 43
7 2 1 2 0 -7.30 40
8 2 1 2 0 -7.10 8
The output expected as:
hr1 hr2 hr3 hr4..... hr24
81 61 53 51 ..... 52
44 50 33 41
One can avoid use of for-loop in this case. An option is to use tidyr::spread to convert your hourly data in wide format.
library(tidyverse)
df %>% select(-t, -wend) %>%
mutate(hr = sprintf("hr%02d",hr)) %>%
spread(hr, heat)
Result:
# wk month dyid hr01 hr02 hr03 hr04 hr05 hr06 hr07 hr08 hr09 hr10 hr11 hr12 hr13 hr14 hr15 hr16 hr17 hr18 hr19 hr20 hr21 hr22 hr23 hr24
# 1 2 1 1 81 61 53 51 30 31 51 90 114 110 126 113 104 107 117 90 114 83 82 61 34 52 41 52
# 2 2 1 2 44 50 33 41 33 43 40 8 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Data:
df <- read.table(text =
"hr wk month dyid wend t heat
1 2 1 1 0 -9.00 81
2 2 1 1 0 -8.30 61
3 2 1 1 0 -7.80 53
4 2 1 1 0 -7.00 51
5 2 1 1 0 -7.00 30
6 2 1 1 0 -6.90 31
7 2 1 1 0 -7.10 51
8 2 1 1 0 -6.50 90
9 2 1 1 0 -8.90 114
10 2 1 1 0 -9.90 110
11 2 1 1 0 -11.70 126
12 2 1 1 0 -9.70 113
13 2 1 1 0 -11.60 104
14 2 1 1 0 -10.00 107
15 2 1 1 0 -10.20 117
16 2 1 1 0 -9.00 90
17 2 1 1 0 -8.00 114
18 2 1 1 0 -7.80 83
19 2 1 1 0 -8.10 82
20 2 1 1 0 -8.20 61
21 2 1 1 0 -8.80 34
22 2 1 1 0 -9.10 52
23 2 1 1 0 -10.10 41
24 2 1 1 0 -8.80 52
1 2 1 2 0 -8.70 44
2 2 1 2 0 -8.40 50
3 2 1 2 0 -8.10 33
4 2 1 2 0 -7.70 41
5 2 1 2 0 -7.80 33
6 2 1 2 0 -7.50 43
7 2 1 2 0 -7.30 40
8 2 1 2 0 -7.10 8",
header = TRUE, stringsAsFactors = FALSE)
With tidyr:
> df<-read.fwf(textConnection(
+ "hr,wk,month,dyid,wend,t,heat
+ 1 2 1 1 0 -9.00 81
+ 2 2 1 1 0 -8.30 61
+ 3 2 1 1 0 -7.80 53
+ 4 2 1 1 0 -7.00 51
+ 5 2 1 1 0 -7.00 30
+ 6 2 1 1 0 -6.90 31
+ 7 2 1 1 0 -7.10 51
+ 8 2 1 1 0 -6.50 90
+ 9 2 1 1 0 -8.90 114
+ 10 2 1 1 0 -9.90 110
+ 11 2 1 1 0 -11.70 126
+ 12 2 1 1 0 -9.70 113
+ 13 2 1 1 0 -11.60 104
+ 14 2 1 1 0 -10.00 107
+ 15 2 1 1 0 -10.20 117
+ 16 2 1 1 0 -9.00 90
+ 17 2 1 1 0 -8.00 114
+ 18 2 1 1 0 -7.80 83
+ 19 2 1 1 0 -8.10 82
+ 20 2 1 1 0 -8.20 61
+ 21 2 1 1 0 -8.80 34
+ 22 2 1 1 0 -9.10 52
+ 23 2 1 1 0 -10.10 41
+ 24 2 1 1 0 -8.80 52
+ 1 2 1 2 0 -8.70 44
+ 2 2 1 2 0 -8.40 50
+ 3 2 1 2 0 -8.10 33
+ 4 2 1 2 0 -7.70 41
+ 5 2 1 2 0 -7.80 33
+ 6 2 1 2 0 -7.50 43
+ 7 2 1 2 0 -7.30 40
+ 8 2 1 2 0 -7.10 8"
+ ),header=TRUE,sep=",",widths=c(5,3,6,5,5,7,5))
>
> library(tidyr)
> df1 <- select(df,dyid,hr,heat)
> df2 <- spread(df1,hr,heat)
> colnames(df2)[2:ncol(df2)] <- paste0("hr",colnames(df2)[2:ncol(df2)])
> df2
dyid hr1 hr2 hr3 hr4 hr5 hr6 hr7 hr8 hr9 hr10 hr11 hr12 hr13 hr14 hr15 hr16 hr17 hr18 hr19 hr20 hr21 hr22 hr23 hr24
1 1 81 61 53 51 30 31 51 90 114 110 126 113 104 107 117 90 114 83 82 61 34 52 41 52
2 2 44 50 33 41 33 43 40 8 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
>
I found solution that helped me to solve my task here: Append data frames together in a for loop
by using empty list and combining later on in data frame
datalist = list()
for (i in 1:24) {
subh= subset(dfSub$heat, dfSub$hr == i)
datalist[[i]] = subh
}
big_data = do.call(rbind, datalist)
both cbind and rbind work.
Thanks everyone for help :)

Use value in colum as argument in function

I have two data frames, one with a list with 3 index variables: User, Log and Pass, and one of which has many values for each of these variables.
I'm trying to pass the many values from the big DF into a list within the smaller DF, so that I can perform summary statistics later.
Small.DF
User,Log,Pass,Valid.Event.Pass
1 11 76 Yes
1 11 46 Yes
1 15 38 Yes
1 15 47 Yes
1 15 386 Yes
1 15 388 Yes
1 8 119 Yes
1 8 120 Yes
1 8 121 Yes
1 8 122 Yes
1 8 123 Yes
1 16 35 Yes
1 16 37 Yes
1 17 22 Yes
1 17 102 Yes
1 12 203 Yes
1 12 205 Yes
1 12 207 Yes
1 12 209 Yes
1 12 24 Yes
2 13 29 Yes
2 1 31 Yes
Big.DF
User,Log,Pass,Passing.Distance
1 11 0 739.5
1 11 0 411.5
1 11 0 0
1 11 0 739.5
1 11 0 0
1 11 0 739.5
1 11 0 0
1 0 0 739.5
1 0 0 0
1 0 0 739.5
1 0 0 0
1 0 0 739.5
1 0 0 0
1 0 0 739.5
1 15 76 371.5
1 15 76 371.5
1 15 76 370.5
1 15 767 368.5
1 15 76 367.5
1 15 76 366.5
1 15 76 365.5
1 15 76 364.5
1 15 76 364.5
1 15 76 363.5
1 15 76 364.5
1 15 76 0
1 15 76 739.5
1 15 76 369.5
1 15 76 0
1 15 76 739.5
1 15 0 0
1 15 0 739.5
1 15 0 0
1 15 0 739.5
1 15 0 0
1 15 0 739.5
1 15 0 0
1 15 0 739.5
1 15 0 0
1 15 0 739.5
1 15 0 0
1 15 0 739.5
1 15 0 0
I'm interested in subsetting the values that match for these three variables in Big.DF but also the 100 values before and 100 values after.
To achieve this I've written a function that will create such a list:
newfn<- function(User,Log,Pass){
test<-subset(Sensor.Data[(min(which(Big.DF$User==User&Big.DF$Log==Log & Big.DF$Pass==Pass))-100):(max(which(Big.DF$User==User&Big.DF$Log==Log & Big.DF$Pass==Pass))+100),],select=Passing.Distance)
}
But I can't figure out how to apply this function over each row in smalldf.
The simplest explanation I can think of would be
Small.df$listofvalues<- newfn(Small.df$User,Small.df$Log,Small.df$Pass)
But that won't work for several reasons I can see....
If it were apply it would be something like this
Small.df$listofvalues<-apply(smalldf,1,newfn)
But this doesn't quite work....and sweep doesn't seem quite right either. Is there any function I'm missing?
Figured it out....
rowfinder<- function(User,Log,Pass){
subset(Sensor.Data[(min(which(Sensor.Data$User==User&Sensor.Data$Log==Log & Sensor.Data$Pass==Pass))-100):(max(which(Sensor.Data$User==User&Sensor.Data$Log==Log & Sensor.Data$Pass==Pass))+100),],select=LH.passing.distance)
}
SmallDF$LHvalues<-apply(SmallDF[,c('User','Log','Pass')], 1, function(y) rowfinder(y['User'],y['Log'],y['Pass']))

Piecewise HLM model using nlme package in R

I have two time periods of interest and four observation points(0 months, 4 months, 12 months, 16 months) for my subjects. The first time period of interest is between observation 1 and observation 3. The second time period of interest is between observation 3 and observation 4.
I would like to run an HLM to account for the correlation of observations on the same subject. I have pasted some sample data and also my code and output are below.
When I compare the model output to actual means they are very similar in this case. However, when I use my actual data set they are less similar. What does this imply? Can you tell me if I have coded time appropriately? My goal is to compare the effect of treatment during time period 1 to the effect of treatment during time period 2. Thank You!
library(nlme)
#Run Model and Get Output
model=lme(Response~Time1*Treatment+Time2*Treatment,
random=~Time1+Time2|Subject,data=test,control=list(opt="optim"))
round(summary(model)$tTable,dig=3)
# Output
Value Std.Error DF t-value p-value
(Intercept) 172.357 2.390 41 72.110 0.000
Time1 0.464 0.062 41 7.496 0.000
Treatment -10.786 3.499 13 -3.083 0.009
Time2 -0.795 0.130 41 -6.113 0.000
Time1:Treatment -0.089 0.091 41 -0.985 0.331
Treatment:Time2 0.563 0.190 41 2.956 0.005
# Means by Treatment and Time vs. Model
mean(test$Response[test$Treatment==1 & test$Observation==1])
[1] 161.1429
#model
172.357-10.786
[1] 161.571
mean(test$Response[test$Treatment==0 & test$Observation==1])
[1] 171.75
#model
[1] 172.357
Sample Data Used for this Output:
Subject Treatment Observation Time Time2 Response
1 0 1 0 0 170
1 0 2 4 0 175
1 0 3 12 0 177
1 0 4 12 4 173
2 1 1 0 0 160
2 1 2 4 0 162
2 1 3 12 0 165
2 1 4 12 4 165
3 0 1 0 0 172
3 0 2 4 0 177
3 0 3 12 0 180
3 0 4 12 4 175
4 1 1 0 0 162
4 1 2 4 0 166
4 1 3 12 0 168
4 1 4 12 4 167
5 1 1 0 0 163
5 1 2 4 0 167
5 1 3 12 0 169
5 1 4 12 4 167
6 0 1 0 0 179
6 0 2 4 0 182
6 0 3 12 0 184
6 0 4 12 4 180
7 0 1 0 0 155
7 0 2 4 0 158
7 0 3 12 0 160
7 0 4 12 4 157
8 1 1 0 0 152
8 1 2 4 0 155
8 1 3 12 0 157
8 1 4 12 4 157
9 0 1 0 0 170
9 0 2 4 0 174
9 0 3 12 0 179
9 0 4 12 4 177
10 1 1 0 0 162
10 1 2 4 0 164
10 1 3 12 0 165
10 1 4 12 4 165
11 1 1 0 0 164
11 1 2 4 0 165
11 1 3 12 0 168
11 1 4 12 4 167
12 0 1 0 0 174
12 0 2 4 0 175
12 0 3 12 0 176
12 0 4 12 4 175
13 0 1 0 0 184
13 0 2 4 0 185
13 0 3 12 0 186
13 0 4 12 4 184
14 1 1 0 0 165
14 1 2 4 0 167
14 1 3 12 0 169
14 1 4 12 4 168
15 0 1 0 0 170
15 0 2 4 0 175
15 0 3 12 0 179
15 0 4 12 4 177
Thanks.

Resources