Liner regression between 2 variables but only in selected lines.

Hi everyone,
I am really beginner in R and it is my first day in this community.

I need help to understand how I can define the command to run a linear regression only in specific lines.

For instance, I have a database with several lines and columns: “country” (1, 2, 3, and 4), “year” (1, 2, 3, 4, 5, 6), “period” (1, 2, 3), “advertising ”, and “sales”.

How can I run linear regression between “advertising” and “sales” but using data only there are country 2, year 3, and period 2?

How can I fit that situation in the command for the model below ?
mod <- lm(sales ~ advertising, data)
summary (mod)

The same situation to run a descriptive analysis (mean, min, max, ...) of sales when, for example, it’s county 1 and period 3.

Basically, I would like to create conditions to run analysis only in specific lines based on different columns.
I don’t know if I explained clearly.

Thank you,

library(tidyverse)
newData <- data %>%  filter( country == 2 & year == 3 & period == 2)

then use newData for your analysis.

I really appreciate your reply

If I need to analyze the period 2 for each country (1, 2, 3, and 4), do I need to analyze separated or I can use a code to give me the result for each country ?

Take a look at this related thread, this shows how to perform linear regressions over nested subgroups of your data frame

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

@andresrcs gives a link for doing it the "right way" in R. In your case you could simplify by a loop like

for (iCountry in 1:4){
     newData <- data %>%  filter( country == iCountry  & period == 2)
# run the regression or whatever here.
}

Fantastic! I really appreciate it
I will try to run it

Does it work too if I want to create a graphs for each country using ggplot?

I believe it is very basic, but as I a very beginner, I am struggling with that.
Thank you

Sure. The code goes through for each country and pulls out one country at a time. You can run a regression, make a plot, or whatever you like using newData.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.