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 9184095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:57:42+00:00 2026-06-17T18:57:42+00:00

I have an R dataframe, strongly simplified as: id <- rep(1:2, c(6,8)) correct <-

  • 0

I have an R dataframe, strongly simplified as:

id <- rep(1:2, c(6,8))
correct <- sample(0:1,14,TRUE)
phase <- c(rep("discr",3),rep("rev",3), rep("discr",4),rep("rev",4))
dat <- data.frame(id,correct,phase)

with id as my subjects (in reality I have a lot more than 2), correct = responses coded as incorrect (0) or correct (1), and the phases Discrimination and Reversal (within-subjects factor).

I want to perform a logistic regression in the form of

glm(correct~phase, dat, family="binomial")

later possibly adding additional predictors.
However, since I have a varying amount of data for each subject, I would like to perform glm() seperately for each subject and later compare the coefficients with ANOVA for group effects.
I would like to do this in a for loop in the form of

for(i in seq_along(dat$id)){
   my_glm[i] <- glm(correct~list,dat[dat$id==i,],family="binomial")
}

but keep receiving the error message

>Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
 contrasts can be applied only to factors with 2 or more levels.

I have checked my data and there is no factor which contains only one level. All subjects gave at least one incorrect and one correct response, and all took part in Discrimination and Reversal. The function works outside the loop when I specify a particular subject.

  • 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-17T18:57:43+00:00Added an answer on June 17, 2026 at 6:57 pm

    you currently trying to do a glm for each row in of id:

    I think you want a glm for each id seperately. Personally, I would go with something like:

    library(plyr)
    
    ddply(dat, .(id), function (x){
    intercept <- coef(summary(glm(correct~phase,family="binomial",data=x)))[1]
    slope     <- coef(summary(glm(correct~phase,family="binomial",data=x)))[2]
    c(intercept,slope)                 
                                  })
    
    # id         V1            V2
    #1  1 -0.6931472  1.386294e+00
    #2  2  1.0986123 -6.345448e-16
    
    # here V1 is intercept and V2 is the estimate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe in R like this: dat = data.frame(Sample = c(1,1,2,2,3), Start
I have a dataframe with distances df<-data.frame(site.x=c(A,A,A,B,B,C), site.y=c(B,C,D,C,D,D),Distance=c(67,57,64,60,67,60)) I need to convert this to
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe which holds an id and a result: df <- data.frame(id=c(1,4,3,3,2,1),result=c(90,75,45,56,78,66))
I have a dataframe such as this one: d <- data.frame(x=1, y=1:10, z=c("apple","pear","banana","A","B","C","D","E","F","G"), stringsAsFactors
I have a dataframe: foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)), df2
I have a dataframe called split2_data (actually a drop-leveled subset of a bigger data
I have a initial dataframe D . I extract two data frames from it
I have a dataframe filled with True and False values, and I'd like to
I have a DataFrame with the following structure: <class 'pandas.core.frame.DataFrame'> DatetimeIndex: 3333 entries, 2000-01-03

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.