# How can I predict logistic model? [closed]

The data is like this.(of course, I have a more data)

week  name reponse x1      x2
5    a      1    1.9    0.5
6    a      1    1.4    0.4
7    a      1    1.62   0.2
8    a      1    1.2    0.1
4    b      0    1      0.2
5    b      0    1.3    0.3
7    b      0    1.4    0.4
8    b      0    0.95   0.3
1    c      1   -0.6    0.2
8    c      1    0.2    0.1


I want to know whether whose response is 1 or 0.

model <- glm(response ~ week + x1 + x2, data = df)
predict(model, new_data, type = "response")


but the output is

  1         2         3         4         5
0.3266134 0.3967968 0.4260996 0.4576125 0.5436105
6         7         8         9        10
0.5428340 0.5596906 0.6288423 0.7670795 0.4516189


I want to print the "a" is (predictions) "b" is (predictions), ... etc, not 1 is 0.3266134, 2 is 0.3967968 .. etc.

How can I fix it?

If the code is right, how can I interpret it?

## closed as off-topic by mdewey, Peter Flom♦Aug 11 at 13:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – mdewey, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

First, your code ignores the binary nature of the response, and the fact that you have multiple observations with the same name. Without knowing details of the variables and the data collection, it is impossible to be sure what is the appropriate model to use.

But assuming name refers to the subjects, and you are measuring the same subjects in different weeks, this is longitudinal data, and you should probably use a mixed effects model.

For example,

library(lme4)
model <- glmer(response ~ week + x1 + x2 + (1 | name),
family=binomial, data=df)


The family=binomial indicates that response is a binary variable. The (1 | name) indicates that the data are repeated measures on the same subjects. The model has a random effect for subjects.

Now to answer your question, it depends what you mean by a prediction. You have different values of the covariates x1 and x2 for each week/subject combination, so you naturally have a different prediction for each row of your data. Just use predict() to get these values. The order of the resulting output is the same as the order of the rows in your data frame.

If you are expecting one value per name, then I assume you want to estimate effects due to the subject rather than predictions. You can get that with ranef(model). However, these will be conditional on the week, x1 and x2 being zero. You can estimate subject effects conditional on other values of week, x1 and x2; a common approach is to set them to their median values.

• I'm sorry. My purpose is to predict name's response(i.e, state) on week9, week 10, ... etc. – 0 Hong Aug 11 at 2:35
• What kind of analysis I do for it? – 0 Hong Aug 11 at 2:43
• Just use predict in that case. The order of the results is the same as the order of your rows. – Rob Hyndman Aug 11 at 3:54