Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:21:38+00:00 2026-06-12T18:21:38+00:00

I’m working on a distributed linear regression calculation in R for Hadoop, but before

  • 0

I’m working on a distributed linear regression calculation in R for Hadoop, but before implementing it, I’d like to verify that my calculations agree with the results of the lm function.

I have the following functions which attempt to implement the generic “summation” framework discussed by Andrew Ng et al. in the paper Map-Reduce for Machine Learning on Multicore.

For linear regression, this involves mapping each row y_i and x_i to P_i and Q_i such that:

P_i = x_i * transpose(x_i)
Q_i = x_i * y_i

Then reducing to solve for the coefficients, theta:
theta = (sum(P_i))^-1 * sum(Q_i)

The R functions to do this are:

calculate_p <- function(dat_row) {
  dat_row %*% t(dat_row)
}

calculate_q <- function(dat_row) {
  dat_row[1,1] * dat_row[, -1]
}

calculate_pq <- function(dat_row) {
  c(calculate_p(matrix(dat_row[-1], nrow=1)), calculate_q(matrix(dat_row, nrow=1)))
}

map_pq <- function(dat) {
  t(apply(dat, 1, calculate_pq))
}

reduce_pq <- function(pq) {
  (1 / sum(pq[, 1])) * apply(pq[, -1], 2, sum)
}

You can implement it on some synthetic data by running:

X <- matrix(rnorm(20*5), ncol = 5)
y <- as.matrix(rnorm(20))
reduce_pq(map_pq(cbind(y, X)))
[1]  0.010755882 -0.006339951 -0.034797768  0.067438662 -0.033557351
coef(lm.fit(X, y))
          x1           x2           x3           x4           x5 
-0.038556283 -0.002963991 -0.195897701  0.422552974 -0.029823962

Unfortunately, the outputs don’t match, so obviously I’m doing something wrong. Any ideas how I can fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:21:39+00:00Added an answer on June 12, 2026 at 6:21 pm

    The inverse you take in reduce_pq needs to be a matrix inverse. Also I changed some of the functions a little bit.

    calculate_p <- function(dat_row) { 
        dat_row %*% t(dat_row)
    }
    
    calculate_q <- function(dat_row) { 
        dat_row[1] * dat_row[-1] 
    }
    
    calculate_pq <- function(dat_row) {
        c(calculate_p(dat_row[-1]), calculate_q(dat_row)) 
    }
    
    map_pq <- function(dat) {
        t(apply(dat, 1, calculate_pq))
    }
    
    reduce_pq <- function(pq) { 
        solve(matrix(apply(pq[, 1:(ncol(X) * ncol(X))], 2, sum), nrow=ncol(X))) %*% apply(pq[, 1:ncol(X) + ncol(X)*ncol(X)], 2, sum)
    }
    
    
    set.seed(1)
    X <- matrix(rnorm(20*5), ncol = 5)
    y <- as.matrix(rnorm(20))
    
    t(reduce_pq(map_pq(cbind(y, X))))
              [,1]      [,2]      [,3]       [,4]        [,5]
    [1,] 0.1236914 0.2482445 0.5120975 -0.1104451 -0.04080922
    
    coef(lm.fit(X,y))
             x1          x2          x3          x4          x5 
     0.12369137  0.24824449  0.51209753 -0.11044507 -0.04080922 
    
    > all.equal(as.numeric(t(reduce_pq(map_pq(cbind(y, X))))), as.numeric(coef(lm.fit(X,y))))
    [1] TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.