12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

$ Var2: Factor w/ 2 levels “sick”,”healthy”: 1 1 2 2$ Freq: num 34 11 9 32The as.data.frame() function converts a table to a data frame in a formatthat you need for regression analysis on count data. If you need to summarizethe counts first, you use table() to create the desired table.Now you get a data frame with three variables. The first two — Var1 and Var2— are factor variables for which the levels are the values of the rows and thecolumns of the table, respectively. The third variable — Freq — contains thefrequencies for every combination of the levels in the first two variables.In fact, you also can create tables in more than two dimensions by addingmore variables as arguments, or by transforming a multidimensional array to atable using as.table(). You can access the numbers the same way you do formultidimensional arrays, and the as.data.frame() function creates as manyfactor variables as there are dimensions.Looking at margins and proportionsIn categorical data analysis, many techniques use the marginal totals of thetable in the calculations. The marginal totals are the total counts of the cases overthe categories of interest. For example, the marginal totals for behavior would bethe sum over the rows of the table trial.table.Adding margins to the tableR allows you to extend a table with the marginal totals of the rows andcolumns in one simple command. For that, you use the addmargins() function, likethis:> addmargins(trial.table)sick healthy Sumrisk 34 9 43no_risk 11 32 43Sum 45 41 86

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!