0

I'm trying to make a loop (or something else that can do this) that can run a linear model of the year and natural log of cases from my data, for each country, separately so that I can gain a slope from each linear model and plot them as a histogram.

I'm very new to R and I'm struggling immensely to work out how to do this; below is a rough snapshot of what my data looks like, and has 197 different countries in total, ranging from years 1997 - 2019.

data

Any help on how to do this would be greatly appreciated, thank you.

  • 1
    Welcome to stack overflow! Please see [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for suggestions on editing your question to improve it. Include data as text, not as an image, and limit stack overflow questions to one specific coding problem instead of asking how to complete an analysis from raw data. – Jan Boyer Dec 13 '19 at 16:18

1 Answers1

0

Based on your question, take a look at this website.

Let's say your data is in a data.frame called df, then you could

df %>%
  split(.$country) %>%
  map(~ lm(log(cases) ~ year, data = .)) %>%
  map(summary) %>%
  map_dbl("Estimate")

Let me know if you need more help with this.

akash87
  • 3,876
  • 3
  • 14
  • 30