Search code examples
rmatlablinear-regressionglm

glmfit in MATLAB vs glm in R.. getting different answers


% A set of car weights\
weight = [0.17. 0.27, 0.44, 0.56, 0.65, 0.79, 0.98, 1.25, 1.56, 2.1, 2.42, 3.02, 3.6, 4.02]';

% The number of cars tested at each weight\
tested = [45 45 45 45 45 45 45 45 45 45 45 45 45 45]';

% The number of cars failing the test at each weight\
failed = [0 0 0 0 4 12 24 37 41 44 45 45 45 45]';

Following are my setup:

IN MATLAB:

X = [tested failed]
y = log(weight)
[beta, std] = glmfit(y, X, 'binomial', 'link', 'probit');

The Result: beta = [-0.6399, 3.2251]

IN Rstudio:

y <- log(weight) 
X <- cbind (tested, failed) 
model <-glm(X~y, family=binomial(link="probit"))

The Result: summary(model) = [-0.905, 0.753]

The results are obviously different and for the life of me I can't seem to figure out why. Any help would be appreciated.


Solution

  • I guess that you misread the doc:

    Matlab:

    % A set of car weights
    x = [0.17, 0.27, 0.44, 0.56, 0.65, 0.79, 0.98, 1.25, 1.56, 2.1, 2.42, 3.02, 3.6, 4.02]';
    % The number of cars tested at each weight
    n = [45 45 45 45 45 45 45 45 45 45 45 45 45 45]';
    % The number of cars failing the test at each weight
    y = [0 0 0 0 4 12 24 37 41 44 45 45 45 45]';
    
    x = log(x);
    [beta, std] = glmfit(x,[y, n], 'binomial', 'link', 'probit')
    
    % beta = [0.0574 3.2919]
    % With glmfit(predictor,[passed tested]... 
    % not  glmfit(predictor,[tested passed]...
    % I'm even surprised that matlab output something else than an error.
    

    R:

    # A set of car weights
    x <- c(0.17, 0.27, 0.44, 0.56, 0.65, 0.79, 0.98, 1.25, 1.56, 2.1, 2.42, 3.02, 3.6, 4.02);
    # The number of cars tested at each weight
    n <- c(45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45);
    # The number of cars failing the test at each weight
    y <- c(0, 0, 0, 0, 4, 12, 24, 37, 41, 44, 45, 45, 45, 45);
    
    model <-glm(y/n~log(x), family=binomial(link="probit"),weights=n)
    
    summary(model)
    
    #Coefficients:
    #            Estimate Std. Error z value Pr(>|z|)    
    #(Intercept)  0.05736    0.09488   0.605    0.545    
    #log(x)       3.29185    0.28049  11.736   <2e-16 ***
    

    Now the model is pretty accurate:

    enter image description here