Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:39:09+00:00 2026-06-15T17:39:09+00:00

Say you have an array like dat <- array(c(126, 100, 35, 61, 908, 688,

  • 0

Say you have an array like

dat <-  array(c(126, 100, 35, 61, 908, 688, 497, 807, 913, 747, 336, 598, 235, 172, 58, 121,402, 308, 121, 215, 182, 156, 72, 98, 60, 99, 11, 43, 104, 89, 21, 36), dim = c(2, 2, 8),dimnames = list(a = c(1, 0), b = c(1, 0), c = 1:8))


> > dat
, , c = 1

   b
a     1  0
  1 126 35
  0 100 61

, , c = 2

   b
a     1   0
  1 908 497
  0 688 807

, , c = 3

   b
a     1   0
  1 913 336
  0 747 598

, , c = 4

   b
a     1   0
  1 235  58
  0 172 121

, , c = 5

   b
a     1   0
  1 402 121
  0 308 215

, , c = 6

   b
a     1  0
  1 182 72
  0 156 98

, , c = 7

   b
a    1  0
  1 60 11
  0 99 43

, , c = 8

   b
a     1  0
  1 104 21
  0  89 36

and you want to fit logistic regression to predict a. Is there a simple way to generate a data frame from this array to use in glm? ie a data frame like

a b c
1 1 1 for 126 rows then
...
0 1 1 for 100 rows, etc.

Basically I need to get data to fit logistic regression when given the table with counts. It seems like there should be a simple way of doing it without manually generating the data.

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:39:10+00:00Added an answer on June 15, 2026 at 5:39 pm

    One way would be to start with the melt function in the reshape2 package:

    library(reshape2)
    
    datM <- melt(dat)
    head(datM, 2)
    #   a b c value
    # 1 1 1 1   126
    # 2 0 1 1   100
    

    Then dcast that data to get the numbers of outcomes on one row:

    dat2 <- dcast(datM, b + c ~ a)
    head(dat2, 2)
    #   b c   0   1
    # 1 0 1  61  35
    # 2 0 2 807 497
    

    You can then use this data to perform a glm where the response is a 2-column matrix giving the numbers of successes and failures:

    response <- as.matrix(dat2[, c(4, 3)])
    bb <- dat2[, "b"]
    cc <- dat2[, "c"]
    glm1 <- glm(response ~ bb + cc, family = binomial(link = "logit"))
    

    However, the model degrees of freedom (and log-likelihood, etc.) will not reflect the data structure you asked for in your question. To get the specific data structure you were aiming for, you could go back to the datM object.

    EDIT:

    The following loops over all columns of datM except for the value column, repeating the values datM$value times:

    datRep <- lapply(datM[-grep("value", names(datM))], rep, times = datM$value)
    

    Then cbind that back into a matrix and convert to data.frame to get the data structure you wanted:

    dat3 <- as.data.frame(do.call(cbind, datRep))
    
    glm2 <- glm(a ~ b + c, data = dat3, family = binomial(link = "logit"))
    

    The coefficients of the two models are the same:

    > coef(glm1)
    (Intercept)          bb          cc 
    -0.43854838  0.77039283 -0.03328575 
    > coef(glm2)
    (Intercept)           b           c 
    -0.43854838  0.77039283 -0.03328575 
    

    But, as mentioned, the degrees of freedom, etc will not be:

    > glm1$deviance
    [1] 29.39535
    > glm2$deviance
    [1] 11381.87
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array that looks like this: array(100) { [0]=> array(4) {
Say I have a dynamic array like: int* integers = new int[100]; Is there
Say I have an array like this: [white, red, blue, red, white, green, red,
Let say I have an array like: Array ( [0] => Array ( [Data]
Let's say I have an array like this: $array = array (this,is,me,and,I,am,an,array,hello,beautiful,world); How can
Say I have an array in PHP that looks like so: $values = Array(
Lets say I have an array with a structure like this: $arr= Array( array(
Say, I have an array that looks like this: var playlist = [ {artist:Herbie
Say I have an object-like data record like this: $article = array( 'title' =>
I have a 2d array, let's say like this : 2 0 8 9

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.