-1

I have a dataset which contain a lot of variable and I need to do linear regression by countries. I have 7 countries here.1

Now, I want to use a loop to do regression separately. Here is my code:

enter image description here

lmcountry(witd_hw$countryest,witd_hw$AverageIncome,witd_hw$sqincome)

While, there seems to have a mistake: object of type 'closure' is not subsettable

but actually I can do the regression like:

lm(witd_hw$countryest~witd_hw$AverageIncome+witd_hw$sqincome,data=witd_hw[witd_hw$Country=="China",])
cramopy
  • 3,459
  • 6
  • 28
  • 42
Vera Cai
  • 3
  • 1
  • 4
    Please do not post images of code, just copy the code and paste as text. Further, please make this question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by adding the output of `dput(head(with_hw))` (or another small representative chunk of data). – r2evans Oct 26 '17 at 18:46

1 Answers1

2

Not with a for loop but with the list apply function. First split the dataframe into sub-frames by country. Then apply lm to each of the subsets:

# Reproducible data example
df = data.frame(x = rnorm(100), y = rnorm(100), country = sample(c('A', 'B', 'C'), 100, replace=TRUE))

list_of_country_dfs = split(df, df$country)

results = lapply(list_of_country_dfs, function(dat) lm(y ~ x, data = dat))
lapply(results, summary)
Fridolin Linder
  • 401
  • 6
  • 12