Multiple linear regression with lm() in R, why is the intersection dependent on the name of the "first" country I have a question about the function lm() used for multiple linear regression analysis.
Context: We have a dataset (that I cannot share) where $y$ is the proportion of women (in some context that I cannot share), then we have some independent variables $x_i$ (numerical values).
Hence I make the command line lm(y~x_1 + x_2 + … +x_n, data = dataset) and that works well. I get a point of intersection n, + more.
However, the dataset allows us to use country as independent variable. For each country at least five points for each independent variable exists. Our hypothesis is that we have a global trend, however er would like to allow for cultural differences in offset. Thus, we are aiming for the model:
y_{country} = a + ( \sum_i  b_i * x_{i,country}) 
    + \epsilon_{country}

Thus, we have the command line lm(y ~ country + x_1 + x_2 + … +x_n, data = dataset).
Problem: My problem is that is seems that the analysis is then dependent on the country that comes first in the alphabet i.e., Australia. In the example with Australia, I get a point of intersection a. But if I change the name of Australia to ex BB, then the output is dependent on Austria instead, and the value of the point of intersection a’ is different from a.
Questions: My questions are: why is the point of intersection dependent on the “first” country? Is there a way to change this? Preferably I would like the “base” intersection of the country specific analysis to be n, and then the (output) list gives the country specific difference from the base intersection (n-a).
I hope my question is clear, and I am sorry, but I cannot share my data with you.
 A: It sounds like the issue here is probably the default behaviour of converting a character variable to a factor variable.  If you have a character variable and you convert it to a factor variable, without specifying the order of the levels, R will order the levels for the factor variable based on alphabetical order of the character strings in the character vector.  To see this, have a look at this example:
#Create character variables for countries
VAR1 <- c('Australia', 'Austria', 'Canada', 'USA')
VAR2 <- c('BB', 'Austria', 'Canada', 'USA')

#Convert these to factors
FACTORVAR1 <- factor(VAR1)
FACTORVAR2 <- factor(VAR2)

#Examine the level ordering for the factor variables
levels(FACTORVAR1)
[1] "Australia" "Austria"   "Canada"    "USA"

levels(FACTORVAR2)
[1] "Austria" "BB"      "Canada"  "USA"

As you can see from this example, when we change the value 'Australia' to 'BB' in the character vector the level for the corresponding factor value goes from first place to second place, even though it remained in the same place in the character vector.  This occurs because, alphabetically, 'Austria' is after 'Australia' but before 'BB'.  Because we did not specify the order of the levels in the factor conversion, the ordering is done using the default behaviour of the factor function, which sorts the character vector to get the levels.

Note: If you call the factor function, you can see the step where it does this, which is this part of the code:
if (missing(levels)) {
        y <- unique(x, nmax = nmax)
        ind <- order(y)        #Observe that this step orders the unique values in x
        levels <- unique(as.character(y)[ind])
     }

A: Apart from changing the reference level with relevel, as suggested by akshaymoorthy, there are two other ways of changing the interpretation of the intercepts per factor level:

*

*Remove the intercept from the formula. lm will then   silently add a dummy variable for the reference level, which effectively results in an absolute intercept value per factor level:

.
> library(MASS)
> coef(lm(Gas ~ Temp +Insul+0, whiteside))
       Temp InsulBefore  InsulAfter 
  -0.336697    6.551329    4.986124



*Use a different level encoding ("contrasts" in R lingo). For contr.sum, e.g., the reference level is the mean of all intercepts:

.
> x <- whiteside
> contrasts(x$Insul) <- contr.sum(length(levels(x$Insul)))
> coef(lm(Gas ~ Temp +Insul, x))
(Intercept)        Temp      Insul1 
  5.7687264  -0.3366970   0.7826023

A: It makes sense that the intercept will change when you re-order a factor variable that is used an independent variable in a general linear model.  The best way to see this is to fit the first model, then fit the model with the factor variable re-ordered.  Then choose some values for country and for the other independent variables, and plug them in to both models, and you'll see that both models predict the same value for the dependent variable.
I don't know if it would satisfy your purposes, but an alternative approach would be to treat country as a random effect, and use a mixed effect model.  In this case the order of the levels of country won't matter.  (And this might be a better approach for your data set.)
library(lme4)

library(lmerTest)

model.1 = lmer(Y ~  IV + (1|Country), data = Data)

summary(model.1)

Edit, addition:

Preferrable I would like the “base” intersection of the country
specific analysis to be n, and then the (output) list gives the
country specific difference from the base intersection (n-a).

The way to do this would be to set the intercept to zero.  (In R, you can do this by including a -1 on the right-hand side of the model formula.)  Usually this isn't a desirable approach. Instead, usually, you would want to use e.g. emmeans() to show you the differences in effect of the levels of e.g. country.
A: All the other responders make valid points.  But it's also important to say that, regardless of the coding used, the differences between countries will remain the same.
The reason the output changes when the "first country" changes is that country is a factor.  A model with both a factor and an intercept is over parameterised.  So (at least) one model parameter can't be estimated.  Different software packages will choose to handle this in different ways.  R chooses not to estmate first factor level, essentially equating this term with the intercept.  Coefficients for other levels of the country factor then get interpreted as " the difference between country x and the first country".
The important thing to realise is that the estimates of "the differences between country x and country y" will reman the same, regardless of the parameterisation chosen.  This is true for all x and y.
A: It's a matter of coding within the regression. Since it's a factor variable, R uses dummy coding automatically, using the first level as reference category (if the factor levels are unordered, then it seems like it uses the alphabetically first level, i.e. Australia). I think you can specify contrasts to get a different effect coding, for example the countries' effects compared to the grand mean.
