1
Subject var1    var2    var3    var4    var5
1   0.2 0.78    7.21    0.5 0.47
1   0.52    1.8 11.77   -0.27   -0.22
1   0.22    0.84    7.32    0.35    0.36
2   0.38    1.38    10.05   -0.25   -0.2
2   0.56    1.99    13.76   -0.44   -0.38
3   0.35    1.19    7.23    -0.16   -0.06
4   0.09    0.36    4.01    0.55    0.51
4   0.29    1.08    9.48    -0.57   -0.54
4   0.27    1.03    9.42    -0.19   -0.21
4   0.25    0.9 7.06    0.12    0.12
5   0.18    0.65    5.22    0.41    0.42
5   0.15    0.57    5.72    0.01    0.01
6   0.26    0.94    7.38    -0.17   -0.13
6   0.14    0.54    5.13    0.16    0.17
6   0.22    0.84    6.97    -0.66   -0.58
6   0.18    0.66    5.79    0.23    0.25
# the above is sample data matrix (dat11)
# The following lines of function is to calculate the p-value (P.z) for a 
# variable pair var2 and var3 using lmer(). 
fit1 <- lmer(var2 ~ var3  +  (1|Subject), data = dat11)
summary(fit1)
coefs <- data.frame(coef(summary(fit1)))
# use normal distribution to approximate p-value
coefs$p.z <- 2 * (1 - pnorm(abs(coefs$t.value)))
round(coefs,6)
# the following is the result
             Estimate    Std.Error    t.value   p.z 
(Intercept) -0.280424   0.110277    -2.542913   0.010993
var3       0.163764     0.013189    12.417034   0.000000

The real data contains 65 variables (var1, var2....var65). I would like to use the above codes to find the above result for all possible pairs of 65 variables, eg, var1 ~ var2, var1 ~var3, ... var1 ~var65; var2 ~var3, var2 ~ var4, ... var2~var65; var3~var4, ... and so on. There will be about 2000 pairs. Can somebody help me with the loop codes and get the results to a .csv file? Thank you.

Steven
  • 11
  • 2
  • Hi, welcome to StackOverflow. Do you have some existing code you can post to show where you've got stuck? Or could you tell us what research you have done and why that didn't help? It is always important on this site to show that you have had a go yourself first. Thanks. – MandyShaw Sep 30 '18 at 17:42
  • I am a biomedical researcher and have very limited programming knowledge. So I don't have any other existing code. – Steven Sep 30 '18 at 17:53
  • No problem, hopefully someone will be able to advise you. – MandyShaw Sep 30 '18 at 17:56
  • Similar question answered nicely here: https://stackoverflow.com/questions/5300595/automatically-create-formulas-for-all-possible-linear-models – Jon Spring Sep 30 '18 at 18:02
  • 1
    You want this: https://stackoverflow.com/questions/51953709/fast-pairwise-simple-linear-regression-between-variables-in-a-data-frame – DanY Sep 30 '18 at 18:14
  • This also looks like a case of p-hacking without any expressed awareness of the validity issues involved. – IRTFM Oct 01 '18 at 02:20

0 Answers0