[R] Problem of intercept?

varin sacha v@r|n@@ch@ @end|ng |rom y@hoo@|r
Wed Feb 22 21:32:29 CET 2023

Dear Peter, Bert and Timothy,

Really appreciated your help. I guess Peter's comments were the nearest of what I was trying to do.
I have tried but it still does not work. I mean the graph is showing the coefficients I am not expecting !
I will try to explain. 
Starting from the equation : Y = b0 + b1*x1 where bo and b1 are the regression beta coefficients representing the intercept and the slope, respectively.
Suppose that, I wish to investigate the differences in Y between males and females. Based on the gender variable, I can create a new dichotomous/dummy variable that takes the value :

1 if a person is male
0 if a person is female

and use this variable as a predictor in the regression equation, leading to the following model :

b0 + b1 if person is male
b0 if person is female

the coefficients can be interpreted as follow:

1. b0 is the average Y among female
2. b0 + b1 is the average Y among male
3. and b1 is the average difference in Y between males and females

The graph with my R code is showing the coefficients I was expecting for multiple regression but not for simple regression. I mean I am expecting to see b0 + b1 for coefficients in the plot for simple regression and the plot is showing me b1 for simple regression ! Is there a solution to my problem?


Le mercredi 22 février 2023 à 10:59:38 UTC+1, peter dalgaard <pdalgd using gmail.com> a écrit : 

Not sure what you are trying to do here.

The immediate issue is that you are getting 'y' on the RHS, because that is the 1st column in Dataset. So "for (i in 2:3)" might be closer to intention. 

However, a 0/1 regresson with no intercept implies that the mean for the "0" group is zero, and with two regressors that the mean is zero for the (0,0) group. Looking at the data, this is quite clearly not the case.

I suppose you may have intended to fit the models _with_ the intercept and then _ignore_ the intercept for plotting purposes, i.e. lm(y~x11+x12, Dataset)$coef[-1], etc.?

(Also, I suspect that you don't actually have y=7 and y=867 in the dataset.)


> On 21 Feb 2023, at 22:33 , varin sacha via R-help <r-help using r-project.org> wrote:
> Dear R-experts,
> Here below my R code working with quite a few warnings. 
> x11 and x12 are dichotomous variable (0=no and 1=yes). I substract 1 to ignore intercept.
> I would like not to ignore intercept. How to modify my R code because if I just remove -1 it does not work?
> y= c(32,45,65,34,23,43,65,76,87,98,7,867,56,45,65,76,88,34,55,66)
> x11=c(0,1,1,0,0,1,1,1,0,0,1,0,0,1,0,0,1,1,0,1)
> x12=c(0,1,0,1,0,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0)
> Dataset=data.frame(y,x11,x12)
> a=lm(y~x11+x12-1,Dataset)$coef
> b=NULL
> for(i in c(1:2)) {
>  f=formula(paste('y~',names(Dataset)[i],-1))
>  b=c(b,lm(f,Dataset)$coef)
> }
> coef=data.frame(rbind(a,b))
> coef$Model=c('Multi','Single')
> library(reshape2)
> coef.long<-melt(coef,id.vars="Model")
> library(ggplot2)
> ggplot(coef.long,aes(x=variable,y=value,fill=Model))+
>  geom_bar(stat="identity",position="dodge")+
>  scale_fill_discrete(name="Model",
>  labels=c("Multiple", "Simple"))+
>  labs(title =paste('La différences des coefficients
>  entre la régression multiple et simple'),
>  x="Models",y="Coefficient")+
>  coord_flip()
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com

More information about the R-help mailing list