# repeated measures in irregular interval

I have glucose measurements of some patients who are visiting in a hospital, which is measured over 4 years time. Each of the patients have more than one record every year. The interval between this records are not same.. i.e. multiple hemoglobin measures of patients in irregular intervals. Can someone please give me a hint on how to find annual mean of glucose measures of total patients. Which function can be used in R to perform this. Thanks

This is arguably more of a question for SO, so it can be migrated.

Let's simulate [glucose] data for $3$ patients and $4$ years with missing values initially coded as 0 - we'll call it dat:

   years    p1    p2    p3
1      1 116.4 106.5  97.4
2      1 101.0 127.4 119.2
3      1   0.0 135.2 102.1
4      1   0.0 108.3 128.3
5      1   0.0 117.0 122.3
6      2 139.3 129.3 130.2
7      2  96.5 103.7 133.0
8      2 100.9 120.9 104.3
9      2   0.0   0.0 114.9
10     2   0.0   0.0  96.6
11     3 118.1 130.3 113.5
12     3   0.0 117.0 116.5
13     3   0.0 134.0 126.9
14     3   0.0   0.0 108.7
15     3   0.0   0.0  97.2
16     4 129.4 139.6 104.6
17     4 117.4   0.0 104.8
18     4 139.8   0.0 111.0
19     4 118.1   0.0   0.0
20     4  97.3   0.0   0.0


Before calculating, it is convenient to turn zeros into NA's:

dat[dat==0] <- NA; dat

   years    p1    p2    p3
1      1  95.8 138.2 122.2
2      1 111.2 117.2 100.6
3      1    NA 124.3 102.1
4      1    NA 115.6  95.7
5      1    NA 121.5 111.4
6      2 102.3 137.0 123.9
7      2 110.1 112.9 119.1
8      2 114.9 100.2  99.1
9      2    NA    NA 120.6
10     2    NA    NA 130.6
11     3 104.1 126.8 102.0
12     3    NA  99.9 131.3
13     3    NA 135.5 135.0
14     3    NA    NA 131.5
15     3    NA    NA 138.0
16     4 120.5 116.0 112.5
17     4 136.5    NA 136.1
18     4 124.6    NA 129.3
19     4  98.2    NA    NA
20     4 134.2    NA    NA


1. Annual mean per patient:

(mean_per_year <- round(aggregate(dat[,2:4], list(dat\$years), mean, na.rm = T), 2))

  Group.1    p1     p2     p3
1       1 103.5 123.36 106.40
2       2 109.1 116.70 118.66
3       3 104.1 120.73 127.56
4       4 122.8 116.00 125.97


2. Annual mean across patients:

round(rowMeans(mean_per_year[,2:4]), 2)

[1] 111.09 114.82 117.46 121.59


3. Total mean:

round(mean(as.matrix(dat[,2:4]), na.rm = TRUE), 2)

[1] 118.01

• Thanks Antoni, one doubt is, as the same patient has multiple measurements, shouldn't we consider the correlation of values of same patient. Dec 2 '15 at 7:20
• Just imagine every point trying to be the geometric center in the data cloud. Identical points would have double 'gravitational' force - count as two. Please check the 'accepted' mark on the side of my answer if it addresses you initial question. Dec 2 '15 at 11:59

I would use lmer in the package lme4. You could then put the patient ID as a random effect.

http://www.inside-r.org/packages/cran/lme4/docs/lmer