How to conditionally loop on a list in r - r

I am not professional with r and need your help with this question - I have a list called result with different items inside as below:
result:
$r
0610007P14Rik 0610009B22Rik 0610009O20Rik 0610010F05Rik 0610010K14Rik 0610011F06Rik 0610012G03Rik 0610030E20Rik 0610037L13Rik
0610007P14Rik 0.00000000 -0.66234644 0.047111033 0.09782589 0.145761085 0.084414075 0.05975822 0.10952475 -0.020151257
0610009B22Rik -0.16234644 0.00000000 -0.227292854 -0.05088201 -0.100237074 0.078595470 -0.12782382 -0.05553298 0.012588413
$p
0610007P14Rik 0610009B22Rik 0610009O20Rik 0610010F05Rik 0610010K14Rik 0610011F06Rik 0610012G03Rik 0610030E20Rik 0610037L13Rik
0610007P14Rik 1.0000000 0.04047111 0.6405067 0.3310033 0.1459042 0.4019556 5.534329e-01 0.2760502 8.417704e-01
0610009B22Rik 0.1047111 1.0000000 0.44868459 0.6139574 0.3191458 0.4353240 2.029875e-01 0.5818857 9.007631e-01
What I want to do is to loop on this list and print pairs if result$r is > then 0.5 or < than -0.5 - and result$p < than 0.05.
So far I have been able to loop through the list by a simple loop where I can print any value at a specific position in the list but have not been able to expand it to perform what I am aiming to:
for (i in 1:length(result)){
print(result[[i]][2])
}
So based on the example above - the output should look like this because this is the only pair that has theresult$r value less than -0.5 and result$p < than 0.05:
0610007P14Rik,0610009B22Rik
Any help is appreciated - thanks.
dput(result)
list(r = structure(c(0, -0.662346440956915, 0.0471110327396697,
0.0978258929200013, 0.14576108466075, 0.0844140746798007, 0.0597582241031368,
0.10952475161027, -0.0201512568819922, -0.162346440956915, 0,
-0.0272928544838261, -0.0508820105675817, -0.100237073610376,
0.0785954698120888, -0.127823820999628, -0.0555329766448806,
0.0125884127823821, 0.0471110327396697, -0.0272928544838261,
0, -0.0565079080178134, 0.13575892944611, 0.0754843375985575,
0.086120417719783, 0.119119974969681, 0.00175356100237076, 0.0978258929200013,
-0.0508820105675817, -0.0565079080178134, 0, 0.131775445763479,
0.053017452395846, 0.0712198787836846, -0.0643888838089917, 0.112498034323393,
0.14576108466075, -0.100237073610376, 0.13575892944611, 0.131775445763479,
0, 0.00774829446850978, -0.0987186269323458, 0.0147657064131866,
0.0300260585042811, 0.0844140746798007, 0.0785954698120888, 0.0754843375985575,
0.053017452395846, 0.00774829446850978, 0, 0.10393741385522,
0.0236032173803311, -0.0182926697871553, 0.0597582241031368,
-0.127823820999628, 0.086120417719783, 0.0712198787836846, -0.0987186269323458,
0.10393741385522, 0, -0.0287458971316651, -0.378837751523345,
0.10952475161027, -0.0555329766448806, 0.119119974969681, -0.0643888838089917,
0.0147657064131866, 0.0236032173803311, -0.0287458971316651,
0, 0.137186835947971, -0.0201512568819922, 0.0125884127823821,
0.00175356100237076, 0.112498034323393, 0.0300260585042811, -0.0182926697871553,
-0.378837751523345, 0.137186835947971, 0), .Dim = c(9L, 9L), .Dimnames = list(
c("0610007P14Rik", "0610009B22Rik", "0610009O20Rik", "0610010F05Rik",
"0610010K14Rik", "0610011F06Rik", "0610012G03Rik", "0610030E20Rik",
"0610037L13Rik"), c("0610007P14Rik", "0610009B22Rik", "0610009O20Rik",
"0610010F05Rik", "0610010K14Rik", "0610011F06Rik", "0610012G03Rik",
"0610030E20Rik", "0610037L13Rik"))), p = structure(c(1, 0.0404711061993476,
0.640506733726655, 0.331003274745838, 0.145904233294019, 0.401955560275036,
0.553432861540715, 0.276050189693768, 0.841770371470927, 0.104711061993476,
1, 0.786845892450507, 0.613957357859433, 0.319145768537945, 0.4353239557619,
0.202987516780359, 0.581885722125544, 0.900763057605725, 0.640506733726655,
0.786845892450507, 1, 0.575261418533015, 0.17603176034094, 0.453788945686311,
0.392462976283337, 0.23580507604055, 0.986141912945937, 0.331003274745838,
0.613957357859433, 0.575261418533015, 1, 0.189217146197476, 0.599137018214154,
0.479787539192647, 0.523038223686805, 0.263117246150375, 0.145904233294019,
0.319145768537945, 0.17603176034094, 0.189217146197476, 1, 0.93882240484224,
0.326580916483781, 0.88370977100432, 0.766081848734211, 0.401955560275036,
0.4353239557619, 0.453788945686311, 0.599137018214154, 0.93882240484224,
1, 0.3014852587724, 0.815110528089105, 0.85620041392968, 0.553432861540715,
0.202987516780359, 0.392462976283337, 0.479787539192647, 0.326580916483781,
0.3014852587724, 1, 0.775787682483708, 7.84039808365529e-05,
0.276050189693768, 0.581885722125544, 0.23580507604055, 0.523038223686805,
0.88370977100432, 0.815110528089105, 0.775787682483708, 1, 0.17147275199326,
0.841770371470927, 0.900763057605725, 0.986141912945937, 0.263117246150375,
0.766081848734211, 0.85620041392968, 7.84039808365529e-05, 0.17147275199326,
1), .Dim = c(9L, 9L), .Dimnames = list(c("0610007P14Rik", "0610009B22Rik",
"0610009O20Rik", "0610010F05Rik", "0610010K14Rik", "0610011F06Rik",
"0610012G03Rik", "0610030E20Rik", "0610037L13Rik"), c("0610007P14Rik",
"0610009B22Rik", "0610009O20Rik", "0610010F05Rik", "0610010K14Rik",
"0610011F06Rik", "0610012G03Rik", "0610030E20Rik", "0610037L13Rik"
))))

You should not loop through the list, as the list is a fixed format and you know that there are the elements $rand $p. Instead, you should loop over the rows and columns you have:
for(row in rownames(result$p)){
for(col in colnames(result$p)){
if(abs(result$r[row,col])>.5 & result$p[row,col]<.05){
print(paste(row,col,sep=", "))
}
}
}
The output is:
"0610009B22Rik, 0610007P14Rik"
This is the only correlation that meets your criterion (r=-.66, p=.040)

You can do the comparison directly if the r and p matrix are of the same dimension.
inds <- which(result$r > 0.5 | result$r < -0.5 & result$p < 0.05, arr.ind = TRUE)
cbind(row = rownames(result$p)[inds[, 1]],
col = colnames(result$p)[inds[, 2]])
# row col
#[1,] "0610009B22Rik" "0610007P14Rik"

Related

Missing value error in the if() condition for R

I want to perform log-normalisation on myy data, and since some enteries are 0.0000 in my dataframe I want to put some very small value of the order 1e-7, so that after performing log normalisation, I don't get -Inf as the value stored.
I'm writing the following code in my console:
for(i in 1:nrow(genes_rpkm_rep_colN))
{
for(j in 1:ncol(genes_rpkm_rep_colN))
{
if(genes_rpkm_rep_colN[i,j] == 0.0000000){
genes_rpkm_rep_colN[i,j] <- 1e-7
}
}
}
I'm encountering the following error while running this piece of code:
Error in if (genes_rpkm_rep_colN[i, j] == 0) { :
missing value where TRUE/FALSE needed
I've put a true/false boolean condition in the if() statement, yet the error.
I'm share a small piece of my data below so that you can have a look and check that my data isn't the one causing the error.
> dput(genes_rpkm_rep_colN[1:10,1:30])
structure(list(X42MGBA_CENTRAL_NERVOUS_SYSTEM = c(0.0093774,
3.99494, 0.0208305, 0.0065619, 0.0084466, 0.0085095, 0.0174268,
0.0233318, 0.0530461, 0.0699613), X8MGBA_CENTRAL_NERVOUS_SYSTEM = c(0,
4.6815, 0.0188461, 0.0118735, 0.0152838, 0.0230965, 0.0157667,
0.0070364, 0.0319951, 0.101274), A1207_CENTRAL_NERVOUS_SYSTEM = c(0.0432576,
2.96619, 0.0137272, 0.0259454, 0, 0.0336463, 0.0114842, 0, 0.0553488,
7.44429), A172_CENTRAL_NERVOUS_SYSTEM = c(0.0194699, 2.92748,
0.0216248, 0.0272483, 0, 0.0176679, 0.0180913, 0.0080738, 0.0665414,
0.0387354), AM38_CENTRAL_NERVOUS_SYSTEM = c(0.0115334, 2.69758,
0.0085399, 0.0322822, 0.0069257, 0, 0.0357226, 0.0063769, 0.0471195,
0.271525), CAS1_CENTRAL_NERVOUS_SYSTEM = c(0.10065, 4.8228, 0.0958194,
0.0469533, 0.0518052, 0.069588, 0.0979765, 0.0556501, 0.117486,
0.147798), CCFSTTG1_CENTRAL_NERVOUS_SYSTEM = c(0.0440228, 6.04641,
0.019558, 0.0246441, 0.0158612, 0.0079897, 0.0163623, 0.0073022,
0.0601819, 0.118238), CH157MN_CENTRAL_NERVOUS_SYSTEM = c(0.0120244,
3.41429, 0.0053421, 0.0235595, 0.0173293, 0.0043646, 0.0044692,
0.0139616, 0.0408118, 0.181811), D283MED_CENTRAL_NERVOUS_SYSTEM = c(0.0638066,
5.12254, 0.0250124, 0.057781, 0.0135231, 0.0272476, 0.0279006,
0.0124515, 0.0583877, 0.343494), D341MED_CENTRAL_NERVOUS_SYSTEM = c(0.0418829,
4.97037, 0.0348888, 0.0219808, 0.0377255, 0.0380065, 0.058376,
0.0217101, 0.0937822, 1.3228), DAOY_CENTRAL_NERVOUS_SYSTEM = c(0.0277923,
4.16543, 0.051447, 0.0194477, 0.016689, 0.0336267, 0.0602569,
0.0460997, 0.0633229, 0.317934), DBTRG05MG_CENTRAL_NERVOUS_SYSTEM = c(0.062215,
4.22423, 0.0307115, 0.0580469, 0.0622661, 0.012546, 0.0128466,
0.0171996, 0.72017, 0.192542), DKMG_CENTRAL_NERVOUS_SYSTEM = c(0.0061458,
2.58862, 0.0546082, 0.0086011, 0.0332147, 0.0446161, 0.0571067,
0.0866511, 0.0985031, 0.128385), GAMG_CENTRAL_NERVOUS_SYSTEM = c(0.0638691,
4.18606, 0.023646, 0.0595902, 0.0095882, 0.0676175, 0.0296734,
0.0264853, 0.0953419, 1.13302), GB1_CENTRAL_NERVOUS_SYSTEM = c(0.0332071,
4.09682, 0.0122941, 0.0232368, 0.0199406, 0.0100446, 0.0205706,
0.036721, 0.15393, 8.77573), GI1_CENTRAL_NERVOUS_SYSTEM = c(0.0236971,
2.99664, 0.0315838, 0.0132657, 0.008538, 0.0344062, 0.0528461,
0.0196535, 0.0826642, 0.132007), GMS10_CENTRAL_NERVOUS_SYSTEM = c(0.112392,
3.29799, 0, 0.0058257, 0.007499, 0.0151096, 0.0232076, 0.0069047,
0.0392457, 0.0786757), GOS3_CENTRAL_NERVOUS_SYSTEM = c(0.0785394,
3.06583, 0.0793018, 0.0349735, 0.0128625, 0.0194374, 0.0464408,
0.0207256, 0.149777, 0.205972), H4_CENTRAL_NERVOUS_SYSTEM = c(0.0412065,
5.11983, 0.0416065, 0.0209705, 0.0337421, 0.0543895, 0.0417697,
0.018641, 0.0953581, 0.432261), HS683_CENTRAL_NERVOUS_SYSTEM = c(0.0395662,
4.82034, 0.0087891, 0.016612, 0.0285111, 0, 0.0294118, 0.0164074,
0.0708759, 0.240087), IOMMLEE_CENTRAL_NERVOUS_SYSTEM = c(0.0089568,
3.07764, 0, 0.0188027, 0.0080677, 0.0406391, 0.0083226, 0.0037142,
0.0295557, 0.178196), KALS1_CENTRAL_NERVOUS_SYSTEM = c(0.0212606,
3.22541, 0.0094454, 0.0059509, 0.0076601, 0.0154343, 0.0790207,
0.0105796, 0.0440979, 0.135353), KG1C_CENTRAL_NERVOUS_SYSTEM = c(0.0306739,
3.25635, 0.0292018, 0.0674589, 0.007894, 0.0397642, 0.0814343,
0.0036343, 0.107415, 0.248463), KNS42_CENTRAL_NERVOUS_SYSTEM = c(0.0377038,
2.77745, 0.0598239, 0.0075381, 0.0097032, 0, 0, 0.0044672, 0.0660162,
0.128592), KNS60_CENTRAL_NERVOUS_SYSTEM = c(0.0308664, 2.75686,
0.0571377, 0.0359982, 0, 0.0186731, 0.0095603, 0, 0.0606269,
0.214931), KNS81_CENTRAL_NERVOUS_SYSTEM = c(0.0376095, 4.39526,
0.041772, 0.0328967, 0.0169382, 0.0341286, 0.0349465, 0.003899,
0.0864295, 0.0841772), KS1_CENTRAL_NERVOUS_SYSTEM = c(0.0113846,
1.91478, 0.0252892, 0.0318656, 0.0102545, 0.0413236, 0.0317354,
0.004721, 0.0295168, 0.18686), LN18_CENTRAL_NERVOUS_SYSTEM = c(0.0159147,
4.40237, 0, 0.0371213, 0.0191134, 0.0192557, 0.0197172, 0.0219985,
0.0600177, 0.358841), LN215_CENTRAL_NERVOUS_SYSTEM = c(0.0188976,
6.19285, 0.0209891, 0, 0, 0.0257228, 0.0175595, 0.0274276, 0.05345,
0.422964), LN229_CENTRAL_NERVOUS_SYSTEM = c(0.0042589, 4.66724,
0.0189209, 0.0059603, 0.0153445, 0, 0.0316585, 0.0070643, 0.0602291,
0.169461)), row.names = c("DDX11L1", "WASH7P", "MIR1302-11",
"FAM138A", "OR4G4P", "OR4G11P", "OR4F5", "RP11-34P13.7", "CICP27",
"AL627309.1"), class = "data.frame")
Maybe try this without a loop:
library(dplyr)
df |>
mutate(across(everything(), ~ifelse(.x == 0.0000000, 1e-7, .x)),
across(everything(), ~log(.x), .names = "log_{col}"))

Getting very large numbers using logistic regression

I'm trying to predict the gender for some samples that have missing values. I'm doing that using the gene expression.
So first I train the logistic regression model using the samples that don't have missing values (that have the value male or female). This is the data I'm using (called mydata) to train the model, and here in the Gender feature, the 1 are males and 0 are females:
structure(list(CA5B = c(30.8594477594147, 30.8773853294407, 31.5109543268185,
29.852812443292, 31.9303544611987, 32.1541109784662, 32.6520127984013,
32.9726252284503, 31.4152036112846, 32.6206677736732), DDX3X = c(35.25792,
35.17134, 36.28966, 36.08013, 36.2734, 35.60448, 36.01073, 36.28618,
35.42917, 35.85764), EIF1AX = c(32.12871, 31.99721, 33.5218,
34.90091, 33.33981, 33.07818, 32.95223, 34.47241, 31.50087, 32.53821
), VAX2 = c(26.0371, 23.2217, 19.53356, 23.92908, 22.51166, 22.45692,
23.62209, 19.53356, 19.53356, 19.53356), KLRC1 = c(30.35354,
28.63985, 25.67501, 26.18108, 30.0377, 29.63008, 25.20041, 28.79883,
30.04889, 31.12243), KLRC2 = c(30.69315, 29.72534, 23.88161,
28.60153, 30.28375, 28.74612, 24.03185, 25.71121, 28.1028, 30.75633
), ARSD = c(31.6010966942421, 31.2081406187661, 32.525989520392,
33.4006989772133, 31.8554455039159, 32.3438989185126, 32.103684088194,
32.2785447752453, 32.028984695614, 31.5829276898759), DDX43 = c(29.90975,
28.0152, 26.15494, 25.70774, 26.4806, 27.44477, 30.52285, 31.97889,
31.50345, 26.90941), RPS4Y1 = c(35.94301, 36.79795, 38.03506,
26.53381, 29.87951, 37.13222, 35.91265, 26.53172, 35.37051, 37.71164
), TRAPPC2 = c(31.73251, 32.12647, 32.91964, 33.16043, 32.28315,
33.24194, 31.20461, 31.56589, 32.482, 34.21314), SNCG = c(28.78017,
33.80945, 31.28264, 35.49992, 31.63203, 29.34577, 29.78785, 30.73165,
29.9412, 26.04425), KDM6A = c(34.19294, 34.71109, 33.94433, 34.64027,
34.93768, 34.25181, 34.2198, 34.88605, 33.38825, 34.8068), ZFX = c(33.84244,
34.04817, 33.83408, 34.90102, 34.77175, 33.54326, 34.39611, 34.50292,
33.27768, 33.87074), PNPLA4 = c(31.15101, 31.32295, 33.38545,
34.34879, 30.98438, 32.77684, 31.26002, 32.36503, 31.15222, 32.12835
), KDM5C = c(33.6612, 34.3589, 33.50819, 34.56994, 34.46354,
33.27832, 34.10299, 34.48084, 34.4775, 34.5186), SMC1A = c(34.18368,
33.39101, 34.2632, 34.28327, 34.15166, 33.94223, 34.71688, 34.61705,
33.99106, 33.76364), DDX3Y = c(34.14224, 34.8835, 34.7245, 26.66744,
29.06797, 34.71189, 33.96947, 26.66531, 34.68055, 34.48187),
SYAP1 = c(32.03834, 32.42337, 32.51431, 33.51916, 32.82407,
32.4735, 32.49154, 33.51064, 31.29551, 31.83166), Gender = c(1,
1, 1, 0, 0, 1, 1, 1, 1, 1)), row.names = c("EA595454", "EA595500",
"EA595522", "EA595529", "EA595597", "EA595624", "EA595632", "EA595635",
"EA595647", "EA595654"), class = "data.frame")
Code:
split = sample.split(mydata, SplitRatio = 0.8)
train_reg = subset(mydata, split == "TRUE")
test_reg = subset(mydata, split == "FALSE")
logistic_model = glm(Gender~., data = train_reg, family = binomial)
predict_reg = predict(logistic_model, test_reg, type = "response")
predict_reg = ifelse(predict_reg >0.5, 1, 0)
This produced AUC of 0.75 (on the test set). Not bad.
Then I take only the samples with the missing values of gender, and predict if they are male or female using the model.
pred = predict(logistic_model,mydata_NA_samples)
This is some of the results I get:
Pt1 Pt10 Pt101 Pt103 Pt106 Pt11 Pt17 Pt18
1548291146811975 -443770882316732 100625892356271 420508521495519 1756507132742650 -883868739619674 -262910227380331 2442533193074350
Pt2 Pt24 Pt26 Pt27 Pt28 Pt29 Pt3 Pt30
569411355627798 1699537030844227 -703783585812457 3495433064250008 -399805416449645 -339035064434972 2024260475793067 109885153661113
Pt31 Pt34 Pt36 Pt37 Pt38 Pt39 Pt4 Pt44
-367070086585505 1330361581729001 1740587250736183 3489930082447853 -976790159879838 1751865170092986 -283113980482947 1902539723154004
Pt46 Pt47 Pt48 Pt49 Pt5 Pt52 Pt59 Pt62
1412716353779596 1108256151592894 1074657527777400 -113959545517722 109187189819909 -57895108035064 792635620314 255566834903770
Pt65 Pt66 Pt67 Pt72 Pt77 Pt78 Pt79 Pt8
-46167159563698 -346701109064255 51185327645114 -795349064523229 244860086302444 4635500642717655 926236606202554 645399266579567
Pt82 Pt84 Pt85 Pt89 Pt9 Pt90 Pt92 Pt94
-651113408988261 -641572344400162 -594901636707441 1514453985992888 -227744411687312 166300730517187 2842003327373200 2502780813663413
I mean, what is this? I'm supposed to get 0 or 1, and maybe some very small number that is close to 0, but this is very strange. I should mention that mydata and mydata_NA_samples have the exact same features, but of course just different samples. How could this happen in logistic regression, which in the first place should only return a binary result?
Thanks!
You forgot to add type="response" in the second predict call.

Double If else condition

I have
sb_bs <- structure(c(1.12091846661435, 1.18009738415273, 1.11298146308129, 1.01543479350125, 1, 0.951241085342297, 1.13080707303053,
1.02485190419514, 1.1642497054576,
1.09758768260489, 1.03143204936253,
0.962263806546468, 1.13646386224131,
1.05528801854607, 1, 1.0099677412001, 1.27733703174185, 1.08617651501537, 1.09642463008668, 0.962776610165752, 1.09650777274647, 1.02585985559578, 1, 0.999386657988493, 1.13956770512708,
1.02801721288972, 1.08668114989057, 1.00619821265949, 1.18651806206661, 1.10567560388514, 1.13871340983327, 1.0641141127524, 1.14138250428664, 1.07165852394532, 1.14672434661463, 1.05773939982737, 1.03068291407157, 0.969220946334891, 1, 0.996545720184137, 1.16201672634516, 1.14932701076931, 1.03239099834277, 0.952596628890447, 1.1015668980208, 1.01070602087167, 1.06446487490351, 0.951035926316207, 1.123214561944, 1.02175192620079, 1.05854401298598, 0.955952082548675, 0.93666054698966, 0.78951026731046, 1.07778659045028, 1.00399655566558, 1.17991276258629, 1.08980598472291, 1, 0.988417315255171,
1.17162519954207, 1.08543591629641, 1.16332270621974, 1.04640496770377,
1.07628139834293, 0.660300975816023, 1.18023510375109, 1.06381322815647, 1, 0.985519330082672, 1, 1, 1.11167375547688, 0.989782753394904, 1.19539027283808, 1.08094458111008, 1.08360592510174, 0.995189438996505, 1.12905282067727, 1.01854704758263, 1, 0.999513001091598, 1.12582107084784,
1.03809878502361, 1.20383394437524, 1.11282621100709, 1.11890798835696,
1.02069677382081, 1.1718663965017, 1.14932701076931, 1.10567893629417,
1.01280861843888, 1, 0.997229184135707, 1.36291711157704, 1.21997211505057, 1, 0.995717127378999,
1.11925862571016, 1.0265000455924, 1.19813504773696, 1.08093802496861, 1.09083155239671, 1.00733022178486, 1.08576682175743, 1.02765522155381, 1.14435310285118, 1.0368424021034, 1.10728085614489, 1.03369127812722, 1, 1, 1, 1.0010343593862, 1.09869128313384, 1.00986110345981, 1.16569766816216, 1.06809300727111, 1.08691881875315, 0.987756631843919, 1, 0.995593919635795, 1.21060416986701, 1.11431887118973, 1.17372870338765, 1.08555976717623, 1.12767749680449,
1.05498213441735, 1.20877696937163, 1.13951692732442, 1.20557208134671,
1.10161115189349, 1.14075335025544, 0.986457883171349, 1.10161115189349,
1.01901529496121, 1, 1.00246940215672, 1.09886441422695, 0.991136014242189, 1.13353139262452, 0.999202603818596, 1.25545764969252, 1.16709504376582, 1.253813300474, 1.18586034201829, 1.36139434951718, 1.27643122050034, 1.11532774927192, 1.0249592726384, 1, 0.98818636452525, 1.11734646265344, 1.0291275567626, 1.09096560662508,
0.995745426192434, 1, 1, 1.10951296399544, 1.01652115456415, 1.16550460579752, 1.11388837421179, 1, 0.997573772176866, 1, 0.98464674069702, 1, 0.997250277028562, 1, 1, 1, 0.999386657988493,
1.12580323330241, 1.0397394609463, 1, 0.999386657988493, 1, 0.997353796391289, 1.14155615975893, 1.05795767043203, 1, 0.972953439553992, 1, 1.00392875483313, 1, 0.995937255059779, 1, 1.00106654711201, 1.08073289044227, 1.02175192620079), .Dim = c(2L, 95L))
and I want run this if-else condition
if ((sb_bs[1,] == 1) || (sb_bs[2,] == 1)) {
rev_sell_buy <- 0
} else {
rev_sell_buy <- sb_bs[1,] - sb_bs[2,]
}
but it considers only the second condition (sb_bs[2,] == 1) in the result.
I need that if the value in the first or in the second row is 1, it return me 0. Else the difference sb_bs[1,] -(sb_bs[2,]).
How can I do that?
Try this
rev_sell_buy <- apply(sb_bs , 2 ,
function(x) if((x[[1]] == 1) | (x[[2]] == 1)) 0 else x[[1]] - x[[2]])
You do not really need any if-else, but could do:
rev_sell_buy <- sb_bs[1,] - sb_bs[2,]
rev_sell_buy[(sb_bs[1,] == 1) | (sb_bs[2,] == 1)] <- 0
Beware of the difference between | and || (see e.g. What is the difference between || and | in R ?)

Drawing Separated Shaded Areas in R Plot

I'm trying to draw a figure of waveforms. I want to show which part of the waveforms are statistically significant. Currently, I draw a semi-transparent polygon:
plot(wave,
type = "l",
col = "red",
lwd = linewid,
pch = 19,
lty = 1,
xlab = "Time",
xaxt = "n",
xlim = c(1,duration/2),
ylab = "Difference",
ylim = c(-1, 1))
abline(h = 0)
sig <- ifelse(pval<.05,wave,0)
polygon(c(1:length(sig)),
sig,
col = rgb(1,0,0,0.5),
border = NA)
But the polygon isn't centered on the zero line of the y-axis. I'd like to shade only the areas underneath the curve of the wave. Instead, the polygon extends above the zero-line on the y-axis. Any ideas?
wave <- c(0.0484408316308237, 0.0474054439781486, 0.0467022242629086,
0.046515614318914, 0.0466686947981267, 0.0466777796491931, 0.0460966374555009,
0.0457341620230469, 0.0455045060507858, 0.0457719372614871, 0.0461446812125276,
0.0460051963987539, 0.0456347093964464, 0.0430700479769684, 0.0435837207487517,
0.0443970279017918, 0.0457508738133201, 0.0472350978374988, 0.0482361020656729,
0.0494006907171422, 0.0504508971582255, 0.0521263688769232, 0.0532433489463588,
0.0537137380543864, 0.0540428548151276, 0.0544949143122896, 0.0544225549891838,
0.0538337952743033, 0.053135984213764, 0.0523491809303349, 0.0520472332622518,
0.0517736309847163, 0.0518684887760298, 0.0514603496453925, 0.050769752723635,
0.0504389714171051, 0.0502927164308292, 0.0504354031597342, 0.0498799936275558,
0.0490825606436222, 0.0497213009991454, 0.0501938355481634, 0.0514117871384259,
0.0519380643522052, 0.0517968505801706, 0.05123157072507, 0.0520909551474945,
0.0486858357936371, 0.0493763701994425, 0.0500160784148426, 0.0505488877007248,
0.0497678090074788, 0.0480758661250716, 0.0462675525180396, 0.0453516919016191,
0.0448339366059345, 0.0445615385738649, 0.044013178561506, 0.0439648543393159,
0.0438670362724258, 0.0440913799994017, 0.0507925460506875, 0.0509727145985309,
0.0510872776847506, 0.0508104967241469, 0.0503812271559447, 0.0503631548556902,
0.0505562349708585, 0.050869650537224, 0.050115073380279, 0.0496307336460131,
0.0486946602966385, 0.0451240814629419, 0.0439636677233932, 0.0428989167765818,
0.0420026704819646, 0.0411584695778936, 0.0403788602838661, 0.040233539087147,
0.0397175149422268, 0.0389289880494877, 0.0378327839257036, 0.0360351888196015,
0.0347926091711749, 0.0341079891575494, 0.0348740749311286, 0.0349125506875405,
0.0352951033387814, 0.0344798859212136, 0.0327899391390952, 0.0303925310234825,
0.0275215845941342, 0.0265832329092289, 0.0220646495463752, 0.0122404036320984,
0.00743877530625988, 0.00181246669131438, -0.00479231506410288,
-0.0117717813867494, -0.0192370027513411, -0.027223713620762,
-0.0348613553743107, -0.0397127268587883, -0.045622681570717,
-0.0515358709254366, -0.0568288397365667, -0.0620165857779051,
-0.0669105535816898, -0.0720264470900791, -0.0766882017731929,
-0.0804427064040755, -0.0815328596670379, -0.0826051939881404,
-0.0879974600724217, -0.0924894198404777, -0.0949544486488778,
-0.104737046247734, -0.11695750473657, -0.132892205151458, -0.15164997657278,
-0.172597865364775, -0.196113512673009, -0.216646106105455, -0.244400723622597,
-0.267988695108909, -0.292598978473393, -0.317086468049069, -0.342530108945073,
-0.368486808868852, -0.399730966642985, -0.433385374917961, -0.469543692326107,
-0.507867318915593, -0.547443797215136, -0.586749203029937, -0.625603126037644,
-0.6626968183054, -0.697811797372003, -0.730226229712439, -0.760716192518167,
-0.789754092566875, -0.819837732291987, -0.844265792897494, -0.865853848085839,
-0.88772546496204, -0.908008383337203, -0.926193346905058, -0.943720637018896,
-0.958657012974673, -0.971195039738284, -0.981680462787076, -0.989209920087862,
-0.994760927508405, -0.998179967730494, -1, -0.99985631234348,
-0.998513746223383, -0.996286337260218, -0.994167673024296, -0.992029087667234,
-0.98942063019129, -0.986657143470197, -0.982080217651251, -0.97535310006632,
-0.967706563058861, -0.959931873177486, -0.953053148939477, -0.945050149326435,
-0.936863678952672, -0.927476791110897, -0.917244708485839, -0.910092208942064,
-0.898659859433262, -0.887894272800133, -0.874966302881798, -0.860464316462603,
-0.843766510522863, -0.826854760379226, -0.809030674702751, -0.790830214526396,
-0.773448702041121, -0.757822022781962, -0.742415284193991, -0.726650963278141,
-0.708671839205669, -0.690647887135473, -0.669566331925841, -0.647484673858103,
-0.625415272964118, -0.603346317669516, -0.580945690740634, -0.559174605387308,
-0.537166524153666, -0.514979755494959, -0.492190905789554, -0.4688961948937,
-0.445648845564897, -0.421370990246327, -0.394957034231288, -0.367257387362894,
-0.339759436905685, -0.31347732732076, -0.287795514335449, -0.263477496955318,
-0.240335559473844, -0.219537822868833, -0.199356394938618, -0.182724128026289,
-0.162855943390834, -0.143113588174923, -0.122168088165277, -0.100967800471397,
-0.0777710229443332, -0.0539643915465976, -0.029677750446946,
-0.00631959566058126, 0.0169258078383277, 0.0389612599575379,
0.0609770118878408, 0.0806172669927091, 0.102073705616963, 0.122665362014863,
0.142328282171209, 0.161475578433955, 0.17913203293436, 0.199700604404855,
0.216864487908698, 0.232810273813389, 0.248031682891701, 0.262732844598723,
0.276791405782004, 0.289592381780554, 0.302904563305743, 0.315933177369042,
0.331194285957781, 0.34328787498597, 0.355317635956366, 0.37161156141851,
0.385496981280364, 0.39906005718835, 0.410609126043194, 0.424557700817611,
0.432614645845991, 0.440840405298792, 0.446859449278095, 0.451067862561763,
0.454108550491332, 0.45822766032593, 0.463669025285741, 0.468751886735504,
0.477222371161998, 0.480825169330004, 0.484778309657409, 0.490686208003411,
0.496271560877119, 0.502429910894803, 0.504627189056216, 0.509277950091572,
0.512796647131139, 0.51920303298796, 0.526246371444159, 0.530172995082546,
0.537137361380815, 0.540646539738041, 0.541350429187775, 0.538037378748107,
0.53640396369579, 0.533982169384575, 0.531715003489143, 0.537771148135836,
0.541774025400292, 0.542397661260989, 0.542734418123511, 0.541900634648552,
0.551336284191385, 0.550830246001875, 0.543935588412506, 0.54850236576883,
0.546095769955119, 0.546560717106744, 0.54862072115046, 0.549873891251691,
0.549022573851835, 0.556546139368112, 0.560110491782052, 0.563328136311772,
0.556118487214394, 0.556145005611977, 0.56177222985652, 0.56211974460993,
0.563139174079545, 0.567595800986669, 0.564911164049346, 0.55370467099592,
0.549142761248365, 0.553638360274585, 0.552297050636191, 0.551520866080127,
0.545782391084414, 0.550845817729848, 0.551708815237408, 0.552680776857495,
0.560806019030281, 0.566586972251016, 0.570035930685996, 0.57778944843747,
0.575407636591647, 0.576215607600804, 0.580856907896507, 0.58111203256702,
0.580550879830819, 0.579468018580888, 0.569996133796829, 0.571472497444831,
0.570186841693214, 0.579257568699911, 0.585984875703449, 0.592864903673866,
0.592890757109184, 0.602046235792163, 0.613343736000507, 0.61690652667541,
0.615073893877543, 0.603386282668406, 0.605140483512513, 0.602317438726602,
0.601349093084652, 0.60175903066173, 0.595748856842631, 0.592466664315233,
0.579486755875179, 0.561987007946437, 0.542492099234415, 0.526383565525105,
0.5230428319822, 0.513300797695766, 0.515049254563791, 0.518848257099875,
0.506235765674015, 0.49998294091854, 0.506344856229246, 0.50475172054043,
0.507702294279798, 0.506348179486846, 0.517341319120319, 0.523551522223028,
0.530756340907612, 0.53032745351512, 0.533111198195776, 0.526453901436172,
0.529598066926201, 0.523624800099041, 0.516232193418245, 0.517039928536979,
0.501904786914197, 0.49713387651536, 0.505916234878408, 0.502395600515955,
0.489414472392961, 0.476329169842886, 0.485088777888902, 0.48219652186471,
0.469886812116599, 0.453441250799586, 0.441168281111148, 0.437074892507931,
0.438771226156789, 0.434582625256592, 0.434393831049118, 0.455313871944686,
0.46667176154786, 0.451614470975422, 0.446531804915084, 0.448747422127997,
0.442389961671837, 0.452345098594434)
pval <- structure(c(0.0237628302370617, 0.0262165284800235, 0.0285686821840486,
0.0297087853681475, 0.0299733840361694, 0.0301202024224222, 0.0323058180308351,
0.0339316553612034, 0.0381144580106053, 0.04487408115707, 0.057804021059368,
0.085158184225468, 0.136972404452296, 0.0594954230338983, 0.059815561375559,
0.0586890211651837, 0.054984325985342, 0.0505545271596015, 0.0488952044969173,
0.0471087059136155, 0.0460746356928649, 0.0424450757403614, 0.0405444189843919,
0.0411829464282132, 0.0412927140116675, 0.0417488854396546, 0.0445117370815736,
0.0550285077721912, 0.0747425914230263, 0.111097457523042, 0.15043556390993,
0.0494268999973122, 0.0517649576685409, 0.0562362352328751, 0.0608441259158306,
0.0635243703413948, 0.0648470007376663, 0.0649911512153626, 0.0678711704637943,
0.0714853336274117, 0.0656394713851027, 0.0631287997230727, 0.0691221286169588,
0.0998026839190638, 0.149978285257545, 0.195667278177658, 0.139258740157594,
0.0641286728653938, 0.0590485245121715, 0.0549751823968685, 0.0525288256855241,
0.0566756473108664, 0.0675151254516982, 0.0797001157496837, 0.0893120076127157,
0.101040948627555, 0.113937187901608, 0.136477809610576, 0.151438694370951,
0.168116109618061, 0.167693574892715, 0.055626851340157, 0.055837522780422,
0.0561583595040639, 0.0594879473890239, 0.0632759635313892, 0.065859251440062,
0.065772893740802, 0.0639366385273011, 0.0671023312185317, 0.0694330478072044,
0.0733540770931381, 0.0927895033698356, 0.0987657684863097, 0.106752925133343,
0.113783571923594, 0.121501003290016, 0.129032949168017, 0.136116836003546,
0.151762019664018, 0.175690091657166, 0.202670406948741, 0.246692883279612,
0.29427387065597, 0.339407794720914, 0.437126245014218, 0.457917873424833,
0.481184291252777, 0.523332782522114, 0.577097846183451, 0.630184174499617,
0.685063979797142, 0.719978579352288, 0.772396520583243, 0.880445896200477,
0.930819867424807, 0.983821279666508, 0.958549578396286, 0.901224976502897,
0.843510707377769, 0.785288039341357, 0.732678879305588, 0.705605278527497,
0.668649736650829, 0.632671877729848, 0.601886683772611, 0.572995378718532,
0.547203236154384, 0.522892433560929, 0.50484004632912, 0.495167623435313,
0.501651238918971, 0.508756506196948, 0.504741256872503, 0.496754657283146,
0.483869763822611, 0.453324086883336, 0.417453905522249, 0.37213400662027,
0.324969615847784, 0.279986826269611, 0.238305111925328, 0.217603724159869,
0.18615434974012, 0.165914268934125, 0.148553539851396, 0.134089539639757,
0.120930326838334, 0.108816102552138, 0.0943727175866119, 0.0809908262000654,
0.0688676027935202, 0.057965295683139, 0.0485177042268327, 0.0406774650506777,
0.0343750114181405, 0.0296041466961569, 0.0257754426846805, 0.0228491378540575,
0.0203762486696415, 0.0182050611089517, 0.0151389244136079, 0.0143291699625514,
0.0135272132937536, 0.0129494727394939, 0.0125413188392993, 0.0120654899893143,
0.0115593848885709, 0.0110971538277043, 0.0106563593895204, 0.0102594799209655,
0.00995618264115798, 0.00976027085065125, 0.00966493285702107,
0.0096809864774456, 0.00984417807568116, 0.0101140178609648,
0.0104536689988165, 0.0107455438459773, 0.0109879390786196, 0.0112476004827354,
0.0115064972197262, 0.0118436117195952, 0.012250241785758, 0.0127098856583327,
0.0131923546938643, 0.0135900513062996, 0.0140907822475909, 0.0146146781894138,
0.0153666265314495, 0.0163587611869966, 0.0165777793501283, 0.0179231032274303,
0.0193146107648153, 0.02112595949975, 0.023429090158608, 0.0263502163261689,
0.029668879819774, 0.0333886702034785, 0.0373851358797521, 0.0414244618086564,
0.0452421641298093, 0.0491730557898139, 0.0534544517392593, 0.0586659192358243,
0.0642835353988069, 0.0717515494462237, 0.0803329713593607, 0.089702211494325,
0.100024733160442, 0.111522126685171, 0.123611882298163, 0.136659951151313,
0.150762549445491, 0.166322127637489, 0.183448713985098, 0.201980927862526,
0.223096893280781, 0.248582577870016, 0.278233697709439, 0.310833681563501,
0.345332013645289, 0.382705067848975, 0.421376239222594, 0.460853428063773,
0.498027141394812, 0.535355843788925, 0.572293181350887, 0.611900790530155,
0.652931049846446, 0.698550246370366, 0.746816303915033, 0.801970397733016,
0.860682735611148, 0.922505922723998, 0.983357734959766, 0.955187832059461,
0.89653477564825, 0.837851886741715, 0.781781567273432, 0.723265191915832,
0.667546011138303, 0.614998358930736, 0.564647511026012, 0.519566323581666,
0.470697912789023, 0.432376848315165, 0.398409375708932, 0.367655920642225,
0.338940852965934, 0.312212428665514, 0.288459290398895, 0.264878592699687,
0.242704842685072, 0.217387439877778, 0.19835325593237, 0.180556517879591,
0.158171361155571, 0.141413156894301, 0.126554452518633, 0.114979049999799,
0.102918102342695, 0.0962216185484208, 0.0901194930496858, 0.0860130895609037,
0.0832340770284259, 0.0812259095628692, 0.078291162078237, 0.0742796112910293,
0.070046963535911, 0.0638805429841068, 0.0600325581294298, 0.0556731158844295,
0.0505107871259209, 0.045748968232586, 0.0409239605265858, 0.038056048177405,
0.0346897761994006, 0.0322017147611021, 0.0290191309178551, 0.0259412764702286,
0.0239876220554575, 0.0213928561132381, 0.0196012142768874, 0.0188687591178971,
0.0191111545249072, 0.0190313087419941, 0.019003849481473, 0.0188195734297014,
0.0172119066683942, 0.015771401215516, 0.0148547142814098, 0.0142875281889536,
0.0138706460080897, 0.0117612653636045, 0.0113802773390233, 0.0119929691882122,
0.011301932221071, 0.0113391829691033, 0.0111974712246918, 0.0109290980999376,
0.0105620733291735, 0.0102904702021988, 0.0088461881724392, 0.00730041492139247,
0.00668346613062251, 0.00766574212937, 0.00783946561969262, 0.00788512630201997,
0.00782058882264157, 0.00767516163055654, 0.00747564784065695,
0.00812968647923146, 0.0091908030084859, 0.00940025876764169,
0.0084452386928757, 0.00773239171673778, 0.00774262750329184,
0.00817566848920374, 0.00775737017229728, 0.00833705507967201,
0.00848219824583305, 0.00775531985314739, 0.00731753429028076,
0.00624994399211075, 0.00566319903650268, 0.00580078551643243,
0.00589284481572378, 0.00557777343775064, 0.00561015236859766,
0.00558045776809902, 0.0062741801741615, 0.00693752198709533,
0.00680148894969647, 0.00682650280136682, 0.00648353640440989,
0.00559647299682892, 0.00560310591626879, 0.00630599774150389,
0.00625849640781645, 0.00496543301213065, 0.00527106355173805,
0.00606524474497961, 0.00782669446225163, 0.00804663337329233,
0.00680216943527376, 0.00549477843115955, 0.00521495965843119,
0.00547975304067046, 0.0058083233283578, 0.00752964959779626,
0.00928425608337011, 0.0121528464847978, 0.0151204119890877,
0.0167466130623839, 0.0184702177828752, 0.0190546966808364, 0.0194217711957074,
0.0172588666752732, 0.0220542021375166, 0.0191777261144048, 0.0207814209894351,
0.0194965472902094, 0.0209260124322778, 0.0170037074001148, 0.0125849197829535,
0.0116502405937885, 0.0120529295967995, 0.0116481500418768, 0.013878269349617,
0.0138614750865797, 0.0153190475349968, 0.0194121459536112, 0.017739167396571,
0.0204726597729821, 0.0245650013533451, 0.0208055307319631, 0.0210348908355722,
0.0278106725250406, 0.035960679421438, 0.0358929798768166, 0.0405937721886476,
0.0510142810649717, 0.0561354348677331, 0.0862664430860822, 0.101130560202344,
0.111832280018731, 0.0774469081405625, 0.0813608282057401, 0.063555234011225,
0.0517641089682014, 0.0663811539378655, 0.0602507226995198, 0.0626085278234381,
0.0676820928084705, 0.0566520960018359), .Dim = c(376L, 1L))
The devil is always in the details with polygon vertices! Using your data from above, here is a solution and some explanation.
Get the polygon vertices, almost
sigy <- ifelse(pval < 0.05, wave, 0)
sigx <- 1:length(wave)
sigxy <- data.frame(sigx, sigy)
This is pretty much what you used in your original question. It doesn't quite work because while the polygon function accepts and connects x,y pairs, it also closes the polygon. Also, your approach was drawing a lot of polygons with zero area. So, some processing of sigxy is necessary to split it into separate polygons. This could be done manually, but it's more fun and useful to automate the process. First, remove the entries where sigy is zero. Then let's replot to see where things stand:
tmp <- sigxy[sigxy$sigy !=0,]
plot(wave, # removed some unnecessary items to simplify
type = "l",
col = "red",
xlab = "Time (by index)",
ylab = "Difference",
ylim = c(-1, 1))
abline(h = 0)
lines(tmp, col = "green")
At this point, we are much closer but are still connecting the dots and not making polygons, since we don't return to the zero line when necessary. Let's create a column that will show us where the polygons should start and stop, then gather that info into a couple of vectors of indices:
tmp$diff <- c(1, diff(tmp$sigx))
st <- 1 # start indices
end <- c() # end indices
for (i in 1:nrow(tmp)) {
if (tmp$diff[i] > 1) end <- c(end, i-1)
if ((tmp$diff[i] > 1) & (i != nrow(tmp))) st <- c(st, i)
if (i == nrow(tmp)) end <- c(end, i-1)
}
Now refresh the plot and add the polygons one at a time
plot(wave, # removed some unnecessary items to simplify
type = "l",
col = "red",
xlab = "Time (by index)",
ylab = "Difference",
ylim = c(-1, 1))
abline(h = 0)
for (i in 1:length(st)) {
DF <- tmp[st[i]:end[i], 1:2] # Just the data to be plotted
# Add the points needed to drop to the zero line
DF <- rbind(c(DF$sigx[1], 0), DF, c(DF$sigx[nrow(DF)],0))
polygon(DF, col = "green")
}
The result:
Bryan Hanson's solution translated into a function:
drawarea <- function(wave, pval, color){
sigy <- ifelse(pval < 0.05, wave, 0)
sigx <- 1:length(wave)
sigxy <- data.frame(sigx, sigy)
tmp <- sigxy[sigxy$sigy !=0,]
tmp$diff <- c(1, diff(tmp$sigx))
st <- 1 # start indices
end <- c() # end indices
for (i in 1:nrow(tmp)) {
if (tmp$diff[i] > 1) end <- c(end, i-1)
if ((tmp$diff[i] > 1) & (i != nrow(tmp))) st <- c(st, i)
if (i == nrow(tmp)) end <- c(end, i-1)
}
for (i in 1:length(st)){
DF <- tmp[st[i]:end[i], 1:2] # Just the data to be plotted
# Add the points needed to drop to the zero line
DF <- rbind(c(DF$sigx[1], 0), DF, c(DF$sigx[nrow(DF)],0))
polygon(DF, col = color, border=NA)
}
}

Iterating over function and get each result value

I created the following function to determine the lag of two variables.
However, this function takes only two parameters, and I would like to run it over my whole dataset:
datSel <- structure(list(stat.resProp.Dwell.4 = c(0.000887705, 0.007954085,
-0.025859667, 0.024097552, 0.114052787, 0.023329207, 0.042143181,
-0.092587287, -0.004050228, -0.001624696, 0.020121403, -0.100502922,
0.057354185, 0.025463388, 0.037409854, 0.001561281, -0.028482938,
-0.004827041, 0.014411779, -0.029034298, 0.021053409, -0.067963182,
0.032070259, -0.038091783, 0.039751534, 0.027802281, -0.027802281,
-0.013355791, 0.009201236, -0.073403679, 0.021277398, -0.033901552,
0.012624153, -0.065733979, 0.032017801, -0.072042665, 0.041936911,
0.002861232, 0.017933468, -0.01698154, 0.006638242, -0.08375153,
-0.007220248, 0.0255507, 0.019980685, 0.013752673, 0.026000502,
-0.021134312, -0.019608471, 0.0166916, -0.021654389, 0.066402455,
0.024828862, -0.083302632, 0.042518482, -0.052439198, 0.037186281,
-0.056311172, -0.012270093), stat.lohn = c(0, -0.007558004, -0.015289567,
0, 0, -0.009609384, -0.019500305, 0, 0, -0.012458015, -0.025391532,
-0.000983501, 0, -0.00165265, -0.003313516, 0.000204576, 0, -0.004898564,
-0.009869709, 0, 0, -0.010574012, -0.021489482, 0, 0, -0.011534651,
-0.023476287, 0, 0, -0.00814845, -0.016498838, 0, 0, -0.0099856,
-0.020275409, -0.002818337, 0, -0.007212389, -0.014582736, 0,
0, -0.004121565, -0.008294445, 0, 0, -0.010766386, -0.021886884,
0, 0, -0.010179741, -0.02067574, 0, 0, -0.011797067, -0.024020039,
-0.002017983, -0.007343864, -0.007398196, -0.014962644), stat.resProp.Dwell.1 = c(0.012777325,
-0.002991775, -0.057819571, -0.00796817, -0.019386714, 0, 0.009740337,
0.005638356, -0.035148694, 0, 0.027084134, -0.160377856, 0.101169235,
-0.043007944, 0.043007944, -0.002580647, -0.015625318, 0.023347364,
0.007662873, -0.09607383, -0.024575906, 0.056733018, -0.000904568,
-0.058703392, 0.011450507, 0.007561473, 0.037879817, -0.032246,
0.042169401, -0.001796946, -0.024580209, -0.148788737, 0.082097362,
-0.000985707, -0.00098668, 0.003940892, -0.049380309, 0.005151995,
0.027371197, -0.025317808, 0.019299736, -0.047382704, -0.010604553,
0.082827084, -0.04516573, 0.003075348, 0.007139245, 0.022111454,
-0.004982571, -0.038701368, 0.018519048, -0.049096021, 0.061254226,
-0.020346582, 0.023363175, -0.00402415, -0.014213437, 0.023245109,
0.027587957), stat.carReg = c(0.022775414, 0.008073857, 0.002624717,
0.169431097, -0.144595366, 0.066716837, -0.086971929, 0.037928208,
0.071752161, -0.046824102, 0.106085873, 0.049965928, -0.057984255,
-0.091650262, 0.090732857, -0.082282389, 0.053376121, -0.044203971,
-0.022855425, 0.025856271, 0.000136493, 0.05579193, -0.293966656,
0.013645739, 0.059732986, 0.187020956, -0.145234848, 0.11041385,
-0.126539687, -0.000949877, 0.031473389, 0.020267816, -0.02180532,
-0.07175183, 0.147500145, -0.040559138, 0.008394819, 0.049045337,
-0.043050615, 0.094358754, -0.058408438, -0.005018402, -0.061717889,
0.100150837, -0.071100417, -0.084393865, 0.002854733, 0.002141389,
-0.026538398, 0.013480513, -0.046002189, -0.030495611, 0.052899746,
0.012842017, 0.064086498, 0.020757573, -0.043441298, -0.009563043,
0.048033848)), .Names = c("stat.resProp.Dwell.4", "stat.lohn",
"stat.resProp.Dwell.1", "stat.carReg"), row.names = c(NA, -59L
), class = "data.frame")
The function and my function call is:
select.lags<-function(x,y,max.lag=8) {
y<-as.numeric(y)
y.lag<-embed(y,max.lag+1)[,-1,drop=FALSE]
x.lag<-embed(x,max.lag+1)[,-1,drop=FALSE]
t<-tail(seq_along(y),nrow(y.lag))
ms=lapply(1:max.lag,function(i) lm(y[t]~y.lag[,1:i]+x.lag[,1:i]))
pvals<-mapply(function(i) anova(ms[[i]],ms[[i-1]])[2,"Pr(>F)"],max.lag:2)
ind<-which(pvals<0.05)[1]
ftest<-ifelse(is.na(ind),1,max.lag-ind+1)
aic<-as.numeric(lapply(ms,AIC))
bic<-as.numeric(lapply(ms,BIC))
structure(list(ic=cbind(aic=aic,bic=bic),pvals=pvals,
selection=list(aic=which.min(aic),bic=which.min(bic),ftest=ftest)))
}
for (i in length(datSel) ) {
for (y in length(datSel) ) {
d1<-ts(datSel[i])
d2<-ts(datSel[y])
lag <- select.lags(d1,d2,5)
}
}
As output of lag I get:
> lag
$ic
aic bic
[1,] -115.3623 -109.56679
[2,] -114.3370 -106.60972
[3,] -116.2026 -106.54350
[4,] -114.7030 -103.11210
[5,] -112.7153 -99.19253
[6,] -110.8018 -95.34721
[7,] -110.0812 -92.69477
[8,] -110.1427 -90.82446
$pvals
[1] 0.1952302 0.3017934 0.7858944 0.9176337 0.5040079 0.0604511 0.3406657
$selection
$selection$aic
[1] 3
$selection$bic
[1] 1
$selection$ftest
[1] 1
As you can see I get only 8 results back, however, my data.frame has 20 variables.
Any recommendation what I am doing wrong?
I appreciate your replies!
If you want to e.g. store the result of the AIC criterion:
lag.aic.store = matrix(NA, 4, 4)
for (i in 1:length(datSel) ) {
for (y in 1:length(datSel) ) {
d1<-ts(datSel[,i])
d2<-ts(datSel[,y])
lag <- select.lags(d1,d2,5)
lag.store.aic[i,y] = lag$selection$aic
}
}
You get 8 values in $ic because max.lag is 8, it has nothing to do with your number of variables.
Please also note that i added commas when indexing by variable for clarity and that you have to loop through 1:length(datSel) as otherwise you will only catch the last variable.

Resources