I'm sorry this may be very simple and stupid but I'm new to R. I've tried everywhere on the net to find a solution but nothing.

I have this data set:

Table <- read.table(text=" age education wantsMore notUsing using 
   <25       low       yes       53     6
   <25       low        no       10     4
   <25      high       yes      212    52
   <25      high        no       50    10
 25-29       low       yes       60    14
 25-29       low        no       19    10
 25-29      high       yes      155    54
 25-29      high        no       65    27
 30-39       low       yes      112    33
 30-39       low        no       77    80
 30-39      high       yes      118    46
 30-39      high        no       68    78
 40-49       low       yes       35     6
 40-49       low        no       46    48
 40-49      high       yes        8     8
 40-49      high        no       12    31", header=TRUE, stringsAsFactors=FALSE)

They show the distribution of 1607 women interviewed according to age, education, desire for more children and current use of contraception.

The task is to understand how contraceptive use depends on age, education, and fertility intentions.

I think I need to use logistic regression with the glm formula but I can't use the formula like this:

glm(using~age+education+wantsMore, family = binomial,data = Table)

Because "using" is not between 0 or 1, it shows frequencies. In class I've always had data set instead of this sort of contingency table. How can I solve this?

If I don't have a column with "0" and "1" I don't really know where to start.

I am really new to R so I apologize again but I really need to sort this out.

I've defined the explanatory variable with two levels: 1=notUsing, 0=using

contraception = as.factor(c(1,0)) 
response <- cbind(notUsing=c(53,10,212,50,60,19,155,65,112,77,‌​118,68,35,46,8,12),
regression <- glm(response~contraception, family=binomial) 

But I have this error:

Error in model.frame.default(formula = response ~ contraception, drop.unused.levels = TRUE) : variable lengths differ (found for 'contraception')

This may not be the solution.

  • 1
    $\begingroup$ These are not individual level data but aggregated data. The way you wrote your regression as of now assumed only 16 cases. A conceptually simpler method is to rearrange it to long form, add a use (yes 1, no 0) and document their cell counts. Use that cell count as frequency weight. Another more coding friendly method is to input the notUsing and Using as a matrix and model it so. This link provides details. $\endgroup$ – Penguin_Knight Apr 21 '17 at 15:09
  • $\begingroup$ I've tried but it's not working (I've edited my post) $\endgroup$ – Kia1995 Apr 21 '17 at 15:44
  • $\begingroup$ This looks like a homework problem, please add the [self-study] tag & read its wiki. In addition, questions that are just about how to use R code are generally off topic here, so you need to clarify the statistical question you have & de-emphasize debugging error messages. $\endgroup$ – gung - Reinstate Monica Apr 21 '17 at 15:55
  • $\begingroup$ Ok I've added the tag. My problem is that I don't even know how to start with the statistical questions because I don't know how to even handle the regression.. $\endgroup$ – Kia1995 Apr 21 '17 at 15:59

This is not a question about statistics, but a good question about R coding. I will give the answer here, but maybe the mods could migrate it to stackoverflow?

OP has the right model and knows hows to fit a logistic regression given individual level data. The trick is to convert the data from a contingency table to a dataframe with one row per individual. Essentially you want to have 53 rows with outcome 0 and covariates age=25, education=low, wantsMore=yes, then 6 rows with same covariate values and outcome 1,... and so on for every different covariate combination. Here is a good snippet of code for the start, from http://www.cookbook-r.com/Manipulating_data/Converting_between_data_frames_and_contingency_tables/:

# Convert from data frame of counts to data frame of cases.
# `countcol` is the name of the column containing the 
countsToCases <- function(x, countcol = "Freq") {
    # Get the row indices to pull from x
    idx <- rep.int(seq_len(nrow(x)), x[[countcol]])

    # Drop count column
    x[[countcol]] <- NULL

    # Get the rows from x
    x[idx, ]

note that you will need to run it twice, because using and notUsing are different columns in your table.

| cite | improve this answer | |

"Using" looks to be a continuous variable. What do the variables "notUsing" and "using" represent? If it is indeed a continuous variable, then you can do a normal linear regression using the Gaussian family, not binomial:

lm(using~age+education+wantsMore, data=Table)

EDIT: Ah, so the image is not the "dataset," but instead a frequency table cut up by all the possible values of the predictors. To run the glm(), you need to either (a) acquire the raw dataset, or (b) convert the table into a dataset.

| cite | improve this answer | |
  • 2
    $\begingroup$ It refers to the use or not of contraception and I need to use logistic regression, no other families. :( $\endgroup$ – Kia1995 Apr 21 '17 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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