1
$\begingroup$

I am newbee and i am trying with functional data analysis. I have a 8x11 matrix data, how can i input into R as an object in this form:

$hgtm

     boy01 boy02 boy03 boy04 boy05
1     81.3  76.2  76.8  74.1  74.2 
1.25  84.2  80.4  79.8  78.4  76.3 
1.5   86.4  83.2  82.6  82.6  78.3  
1.75  88.9  85.4  84.7  85.4  80.3 
2     91.4  87.6  86.7  88.1  82.2  
3    101.1  97.0  94.2  98.6  89.4 
4    109.5 104.6 100.4 104.4  96.9 
5    115.8 112.3 107.1 111.0 104.1 

$hgtf

     girl01 girl02 girl03 girl04 girl05 
1      76.2   74.6   78.2   77.7     76   
1.25   80.4   78.0   81.8   80.5     80   
1.5    83.3   82.0   85.4   83.3     83  
1.75   85.7   86.9   87.9   87.0     86   
2      87.7   90.0   89.6   90.3     89   
3      96.0   94.9   97.1   98.6     96  
4     103.8  102.1  109.2  106.3    103   
5     110.7  109.2  116.2  113.9    110  

 $age

 [1]  1.00  1.25  1.50  1.75  2.00  3.00  4.00  5.00

very much appreciated,

$\endgroup$
  • 2
    $\begingroup$ Could you try formating your data so that we can get a clearer idea of what you want. For example, I don't see an 8x11 matrix in your example. $\endgroup$ – csgillespie Oct 18 '10 at 13:52
  • $\begingroup$ Thanks for quick reply, I am trying format matrix in this windows but quit difficult. I describe my matrix in word: the 8x11 matrix comprise of 8 rows corresponding to ages (1, 1.25,1.5,1.75,2,3,4,5) and 11 columns (first column name "age", then boy01...boy05, girl01...girl05) the values of matrix is the height of 5 boys and 5 girls. $\endgroup$ – Nam Van Oct 18 '10 at 14:07
  • $\begingroup$ I do not know how to present the height of group boy as "$hgtm", group girl as "hgtf" and age column as "$age" as display above. $\endgroup$ – Nam Van Oct 18 '10 at 14:15
  • $\begingroup$ At this point, I'm a little confused about the data structure too. Maybe describe what you're trying to do? You have a set of boys and girls, along with their respective heights and ages? Are the heights measured at different ages? $\endgroup$ – Shane Oct 18 '10 at 14:29
  • 2
    $\begingroup$ As it hasn't been mentioned before, there are introductory manuals that come with R. The Introduction will teach you the basics of data structure use (and more); the Data Import/Export is helpful for data transfer questions. $\endgroup$ – Dirk Eddelbuettel Oct 18 '10 at 14:44
4
$\begingroup$

As to how you would get the data into R and into one of these formats, we'd need to know more about what your input file looks like and the format that it is in. However, here are some general tips on formatting the type of data that you have for analysis in any system.

Singer (Applied Longitudinal Data Analysis) suggests two generally useful layouts for the statistical analysis of longitudinal data: the person-level (mutlivariate) structure or the person-period (univariate) structure. The latter is generally preferrable for a number of reasons.

The person-level data structure (or the multivariate format) contains one row of data for each observational unit (such as persons) and a variable for each measurement period. Age would not be included in the data set and would be implicit in the levels of your time factor (e.g., in a repeated measures ANOVA). This structure can lead naturally to summaries that aren't very meaningful, is less efficient than it could be, and cannot account for your unequally spaced observations (differing age intervals between observations) or time-varying covariates.

That data setup might look something like this...

Mutlivariate
    ID  Gender  height1 height2 height3 height4 height5
    1   Boy     76.2   74.6   78.2   77.7     76
    2   Boy     80.4   78.0   81.8   80.5     80
    3   Boy     83.3   82.0   85.4   83.3     83
    4   Girl    96.0   94.9   97.1   98.6     96
    5   Girl    87.7   90.0   89.6   90.3     89
    6   Girl    85.7   86.9   87.9   87.0     86

A preferable layout is often the person-period layout (or the univariate format) where each individual has a record for each time for which they were observed. The person-period dataset has a number of advantages. First, it leads to more natural summaries of the data, e.g. getting an average by group, by time or by group and time is now straight forward. Second, the dataset will accommodate entry of unequal intervals in the time dimension, such as you have here. In addition, if you have them, you could add columns for any other demographic covariates and these could differ over time. Also, data in this format is prepared for modern analytical techniques such as multilevel modeling. Finally, the univariate data structure is consistent with good practice in database design and normalization, increasing efficiency and making it appropriate for the typical query structure.

The univariate layout would look something like this...

Univariate
    ID  Age Gender  Height
    1   1   Boy     76.2
    1   1.5 Boy     74.6
    1   3   Boy     78.2
    1   5   Boy     77.7
    2   1   Girl    80.4
    2   1.5 Girl    81.8
    2   3   Girl    80.5
    2   5   Girl    80
    3   1   Boy     115.8
    3   1.5 Boy     112.3
    3   3   Boy     111.0
    3   5   Boy     104.1
$\endgroup$
  • 2
    $\begingroup$ Another point in favor of the second layout is that it is consistent with good database practices. The data normalization advantages of databases and a knowledge of SQL is very useful if used appropriately. $\endgroup$ – user28 Oct 18 '10 at 17:10
  • $\begingroup$ @Srikant: Thanks, I've updated my post to include. $\endgroup$ – Brett Oct 18 '10 at 18:20
3
$\begingroup$

It looks like you want to use a named list, since each object is of different dimensions above. Here is an example with some dummy data:

hgtm <- matrix(1:100, ncol=5)
hgtf <- matrix(1:100, ncol=5)
age <- 1:10
namvan <- list(hgtm=hgtm, hgtf=hgtf, age=age)

Now you can reference each object by name:

> str(namvan)
List of 3
 $ hgtm: int [1:20, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
 $ hgtf: int [1:20, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
 $ age : int [1:10] 1 2 3 4 5 6 7 8 9 10
> namvan$age
 [1]  1  2  3  4  5  6  7  8  9 10
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.