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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:41:00+00:00 2026-05-25T18:41:00+00:00

How can I use ddply function for linear model? x1 <- c(1:10, 1:10) x2

  • 0

How can I use ddply function for linear model?

x1 <- c(1:10, 1:10)
x2 <- c(1:5, 1:5, 1:5, 1:5)
x3 <- c(rep(1,5), rep(2,5), rep(1,5), rep(2,5))

set.seed(123)
y <- rnorm(20, 10, 3)
mydf <- data.frame(x1, x2, x3, y)

require(plyr)
ddply(mydf, mydf$x3, .fun = lm(mydf$y ~ mydf$X1 + mydf$x2)) 

This generates this error:

Error in model.frame.default(formula = mydf$y ~ mydf$X1 + mydf$x2,
drop.unused.levels = TRUE) :
invalid type (NULL) for variable ‘mydf$X1’

Appreciate your help.

  • 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-25T18:41:01+00:00Added an answer on May 25, 2026 at 6:41 pm

    Here is what you need to do.

    mods = dlply(mydf, .(x3), lm, formula = y ~ x1 + x2)
    

    mods is a list of two objects containing the regression results. you can extract what you need from mods. for example, if you want to extract the coefficients, you could write

    coefs = ldply(mods, coef)
    

    This gives you

      x3 (Intercept)         x1 x2
    1  1    11.71015 -0.3193146 NA
    2  2    21.83969 -1.4677690 NA
    

    EDIT. If you want ANOVA, then you can just do

    ldply(mods, anova)
    
      x3 Df    Sum Sq   Mean Sq   F value     Pr(>F)
    1  1  1  2.039237  2.039237 0.4450663 0.52345980
    2  1  8 36.654982  4.581873        NA         NA
    3  2  1 43.086916 43.086916 4.4273907 0.06849533
    4  2  8 77.855187  9.731898        NA         NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get a data frame's name from a list? Sure, get() gets
R: how can I populate the rows of a data frame, in which each
I have a dataframe: > df <- data.frame( + Species = rep(LETTERS[1:4], times=c(5,6,7,6)), +
I can use the following code for tiny little queries: DECLARE @sql VARCHAR(8000) SET
I can use SELECT * FROM table WHERE id IN (478,278,190,890,123) to return a
How can use the key argument for the min function to compare a list
I can use GTK allocation function, but that only gives the position relative to
Flash can use the RTMP protocol to exchange data between a server and a
How can I efficiently extract group-wise constant columns from a data frame? I've included
I can use set modDate to the modification date of theFile as string to

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.