Github user iyounus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10274#discussion_r49140607
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/optim/WeightedLeastSquares.scala ---
    @@ -94,8 +110,7 @@ private[ml] class WeightedLeastSquares(
           if (standardizeFeatures) {
             lambda *= aVar(j - 2)
           }
    -      if (standardizeLabel) {
    -        // TODO: handle the case when bStd = 0
    +      if (standardizeLabel && bStd != 0) {
    --- End diff --
    
    @dbtsai The problem here is that for regularized regression in R, I need to 
use `glmnet`. But for this specific case (constant label, no intercept and no 
regularization) the results from `glmnet` do no match with `lm`. So I see a 
discrepancy within R itself. Have a look at the following R code:
    
    ```
    A <- matrix(c(0, 1, 2, 3, 5, 7, 11, 13), 4, 2)  
    b <- c(17, 17, 17, 17)  
    w <- c(1, 2, 3, 4)  
    df <- as.data.frame(cbind(A, b))
    
    lm.model <- lm(b ~ . -1, data=df, weights=w)
    print(as.vector(coef(lm.model)))
    [1] -9.221298  3.394343
    
    glm.model <- glmnet(A, b, weights=w, intercept=FALSE, lambda=0,
                        standardize=FALSE, alpha=0, thresh=1E-14)
    print(as.vector(coef(glm.model)))
    [1] 0 0 0
    ```
    
    Note that in this example, I expect same results from both `lm` and 
`glmnet` because I've set `lambda=0` in `glmnet`. (BTW `standardize` has not 
effect here.) It seems to me that `glmnet` just sets all coefficients to zero 
if label is constant and intercept is not included. This is true even if I 
include regularization.
    
    Right now `WeightedLeastSquares` (without regularization) matches with 
`lm`, and I think this is the correct behaviour given my understanding of the 
normal equation. With regularization, it should still give some non-zero 
coefficients, which is does. I don't know why `glmnet` behaves differently, but 
I don't think we should try to match that in this particular case.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to