Skip to content

Instantly share code, notes, and snippets.

@msallin
Last active September 8, 2021 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msallin/06f0227d6468a252e270ca7f72992cc2 to your computer and use it in GitHub Desktop.
Save msallin/06f0227d6468a252e270ca7f72992cc2 to your computer and use it in GitHub Desktop.
example_annik
participant_id <- c(1,1,2,2,3,3,3,4,4)
answer1 <- c(12,123,33,333,3,22,11,32,12)
answer2 <- c(12,123,33,333,3,22,11,22,11)
results <- data.frame(participant_id, answer1, answer2)
# Results looks like this:
# participantId answer1 answer2
# 1 1 12 12
# 2 1 123 123
unique_participant_ids <- unique(results$participant_id)
# unique_participant_ids looks like "[1] 1 2 3 4"
intercept_per_participant <- c()
for (participantId in unique_participant_ids) { # Iterate over all participants
all_answers_for_participant <- subset(results, participant_id == participantId) # Get only the answers from the current participant
# fit here your model with "all_answers_for_participant"
# E.g. https://stackoverflow.com/questions/23240182/deciding-threshold-for-glm-logistic-regression-model-in-r
model <- lm(all_answers_for_participant$answer1 ~ all_answers_for_participant$answer2) # I use LM as an example
model_summary <- summary(model)
intercept_value <- model_summary$coefficients[1,1]
intercept_per_participant <- c(intercept_per_participant, intercept_value) # Append the interesting things to a list (here intercept)
}
# Now do something with your data
# E.g. get the man of all intercepts..
mean(intercept_per_participant)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment