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 6193999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:13:09+00:00 2026-05-24T03:13:09+00:00

I have a simple matrix like: [,1] [,2] [,3] [1,] 1 2 3 [2,]

  • 0

I have a simple matrix like:

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12

I have to calculate a linear regression of these columns, like: lm(x ~ y)

Where thefirst column is the X, and the other are the Y? I mean… can I do something to use the other with one variable(y)

or

do i have to use something like: lm(x~y+z+c+b) etc etc ?

Thank you

  • 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-05-24T03:13:10+00:00Added an answer on May 24, 2026 at 3:13 am

    Yes, but I wouldn’t really recommend it:

    > set.seed(2)
    > mat <- matrix(runif(12), ncol = 3, byrow = TRUE)
    > mat
              [,1]      [,2]      [,3]
    [1,] 0.1848823 0.7023740 0.5733263
    [2,] 0.1680519 0.9438393 0.9434750
    [3,] 0.1291590 0.8334488 0.4680185
    [4,] 0.5499837 0.5526741 0.2388948
    > mod <- lm(mat[,1] ~ mat[,-1])
    > mod
    
    Call:
    lm(formula = mat[, 1] ~ mat[, -1])
    
    Coefficients:
    (Intercept)   mat[, -1]1   mat[, -1]2  
         1.0578      -1.1413       0.1177
    

    Why is this not recommended? Well, you are abusing the formula interface here; it works but the model coefficients have odd names and you are incurring a lot of overhead of working with the formula interface, which is designed for extracting response/covariates from a data frame or list object referenced in the symbolic formula.

    The usual way of working is:

    df <- data.frame(mat)
    names(df) <- c("Y","A","B")
    ## specify all terms:
    lm(Y ~ A + B, data = df)
    ## or use the `.` shortcut
    lm(Y ~ ., data = df)
    

    If you don’t want to go via the data frame, then you can call the workhorse function behind lm(), lm.fit(), directly with a simple manipulation:

    lm.fit(cbind(rep(1, nrow(mat)), mat[,-1]), mat[, 1])
    

    here we bind on a vector of 1s to columns 2 and 3 of mat (cbind(rep(1, nrow(mat)), mat[,-1])); this is the model matrix. mat[, 1] is the response. Whilst it doesn’t return an "lm" classed object, it will be very quick and can relatively easily be converted to one if that matters.

    By the way, you have the usual notation back to front. Y is usually the response, with X indicating the covariates used to model or predict Y.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 matrix structs means equal data but have different form like these:
I have a matrix of one column and 6 rows. I would like to
I have a simple matrix like: > a = matrix(c(c(1:10),c(10:1)), ncol=2) > a [,1]
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
Let's assume that we have a simple matrix 3rows x 7cols. The matrix includes
We have a simple table with say (nxm matrix) and the user will randomly
ok it seems like a simple problem, but i am having problem I have
I have a simple array, sort of like this: 1 2 3 4 5
I would like to move an object (I have a simple rectangle) and I
I have a simple 2D array of strings and I would like to stuff

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.