mql4beginner
mql4beginner

Reputation: 2243

How to write a code that will create glm models based on different filters and attach them to the original data frame

I would like to run a code that will create different glm model based on a filter on the target variable so I'll get different model objects and results per each filter.The filter is on one variable only and I have 14 filters to run. Here is my example data and toy code:

dat1 <- read.table(text = " target wolfs     snakes
                    3        1         7
                    3        0         4
                    1        0         8
                    1        1         3
                    1        0         3
                    6        1         2
                    6        0         1
                    6        1         5
                    5        0         7
                    3        0         7
                    4        1         7
                    1        0         3
                    7        1         3
                    6        0         1
                    6        0         9
                    6        1         1   ",header = TRUE)

I would like to run a glm model where target==0 to target==14 and then target==21 and target==30.Then I would like to add the prediction of each model to the original model like in this output (for example: target variable ==0 ):

dat1 <- read.table(text = " target wolfs  snakes  target_0 target_1
                        3        1     7       0         1
                        2        0       4       0         1
                        0        0       8       0         0
                        1        1       3       1         1
                        14       0        3      1         0
                        6        1       2       0         1
                        6        0       1       0         1
                        6        1       5       1         1
                        5        0       7       1         0
                        3        0       7       1         0
                        30       1        7      1         1
                        1        0       3       0         0
                        7        1       3       0         1
                        6        0       1       0         0 
                        6        0       9       0         0
                        6        1       1      1  0 ",header = TRUE)

I'm new to code writing so I created this toy code but It doesn't work obvisuly. Any help will be great.

for (i in c(1:14,21,30)) function (x) { ifelse{dat$target==[[i]],
                                               paste0("glm","[[i]]")<-glm(,data=dat, family=binomial(link='logit')
                                           paste0("dat$","[[i]]") <-ifelse(predict(glm[[i]],newdata=data[which(dat$target==[[i]],),])>0.5,1,0) 
}

Upvotes: 2

Views: 1135

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

Your example doesn't run so I can't verify that it works. You can try this.

for (i in c(0:14, 21, 30)) {
    my.mdl <- glm(wolfs ~ snakes, data = dat1, subset = dat1$target == i)
    dat1[dat1$target == i, paste("target", i, sep = "")] <- predict(my.mdl)
}

Upvotes: 1

Related Questions