I want to run a simple logit regression in R, where my dependent variable is whether a firm charges a positive price or not, and the key independent variable is number of competitors within an x mile radius of the firm. To operationalize the competition variable, I am looking at 1, 5, 10 and 50 miles radius.
I am not sure how to write the loop though, and the Error in eval(predvars, data, env) : object 'radius_i' not found
when I run the loop below.
circle_radius = list("1", "5", "10", "15", "50")
for (i in seq_along(circle_radius)){
my_logit_4_r[i] <- glm(price_b1 ~ radius_i ,
data=data1,
family = binomial(link='logit'))
summary(my_logit_4_r[i])
}
So I am not sure how to specify the loop, as I do not want to use brute force and write the 4 regressions separately. Would appreciate help on what error I am making.
CodePudding user response:
You have to change your code a bit - first, use get()
to use the what you are trying to call from radius_[i]
to be a covariate in your model - though this needs to be changed to get(paste0("radius_",i))
(assuming you have a covariate named radius_1
, radius_5
, and so on in your data1
data frame. Also, you might want to remove the seq_along(circle_radius)
and just do circle_radius
since seq_along
will define i
as 1, 2, 3, 4 and removing it will define it as "1", "5", "10", and "50". You also need to define my_logit_4_r
as a list and use double bracket [[i]]
when assigning to the list in the loop.
Below I have made the changes to make this clearer.
Since you didnt provide sample data, I am assuming your data look like this:
circle_radius <- list("1", "5", "10", "50")
data1 <- data.frame(price_b1 = runif(100),
radius_1 = runif(100),
radius_5 = runif(100),
radius_10 = runif(100),
radius_50 = runif(100))
Try the following code:
my_logit_4_r <- vector(mode = "list", length = length(circle_radius))
for (i in circle_radius){
my_logit_4_r[[i]] <- glm(price_b1 ~ get(paste0("radius_",i)) ,
data=data1,
family = binomial(link='logit'))
summary(my_logit_4_r[[i]])
}
The models wont converge with my sample data, but they attempt to run. If this doesn't work, please provide sample data and I update my answer.