简体   繁体   中英

Linear regression by group using loops

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:

在此处输入图片说明

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",])

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM