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

  • Home
  • SEARCH
  • 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 9072785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:15:18+00:00 2026-06-16T18:15:18+00:00

I’m currently trying to build an LDA model on a dataset which contains some

  • 0

I’m currently trying to build an LDA model on a dataset which contains some missing (NA) values. I want to, for example, impute the mean for NA values. From what I understand, I can set na.action=na.omit in the lda and predict functions which will remove the observations when building the model, and force return of NA when making predictions.

my.dat <- as.data.frame(cbind(
    c(0, 1, 0, 1, 1, 0),
    c(5, 8, 9, 1, -1, NA),
    c(-2.4, -4.0, -4.4, -0.5, 0.7, -0.3)
))
mod <- lda(my.dat[,-1], my.dat[,1], na.action=na.omit)
predict(mod, my.dat[,-1], na.action=na.omit)

But I want now to impute the means where I have an NA value. So, I can define my own na.impute function. But, I cannot understand what is passed to this function, and what I need to return.

na.impute <- function (object) {
    print(object)
    object
}

which gives me output:

[1] g x
<0 rows> (or 0-length row.names)

which doesn’t make much sense to me. I cannot find any guidance in the documentation. What exactly is object, and how am I supposed to manipulate it to overwrite NA values?

  • 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-16T18:15:20+00:00Added an answer on June 16, 2026 at 6:15 pm

    Here is the first way how to find out what is object:

    na.impute <- function (object) {
      browser()
      print(object)
      object
    }
    
    lda(my.dat[,-1], my.dat[,1], na.action=na.impute)
    # Called from: na.action(structure(list(g = grouping, x = x), class = "data.frame"))
    Browse[1]> str(object)
    # 'data.frame': 0 obs. of  2 variables:
    #  $ g: num  0 1 0 1 1 0
    #  $ x: matrix [1:6, 1:2] 5 8 9 1 -1 NA -2.4 -4 -4.4 -0.5 ...
    #   ..- attr(*, "dimnames")=List of 2
    #   .. ..$ : NULL
    #   .. ..$ : chr  "V2" "V3"
    Browse[1]> object$g
    # [1] 0 1 0 1 1 0
    Browse[1]> object$x
    #      V2   V3
    # [1,]  5 -2.4
    # [2,]  8 -4.0
    # [3,]  9 -4.4
    # [4,]  1 -0.5
    # [5,] -1  0.7
    # [6,] NA -0.3
    # attr(,"class")
    # [1] "matrix"
    

    So it really is an unusual object: structure(list(g = grouping, x = x), class = "data.frame"). Another way to see this, let us inspect function lda:

    lda
    # function (x, ...) 
    # UseMethod("lda")
    # <bytecode: 0x0e3583fc>
    # <environment: namespace:MASS>
    methods(lda)
    # [1] lda.collapsed.gibbs.sampler lda.data.frame*             lda.default*               
    # [4] lda.formula*                lda.matrix*                
    # 
    #    Non-visible functions are asterisked
    

    In this case we are interested in lda.data.frame. Since it is asterisked we have to use either MASS:::lda.data.frame or getAnywhere("lda.data.frame") to see the source code:

    function (x, ...) 
    {
        res <- lda(structure(data.matrix(x), class = "matrix"), ...)
        cl <- match.call()
        cl[[1L]] <- as.name("lda")
        res$call <- cl
        res
    }
    <bytecode: 0x067c3248>
    <environment: namespace:MASS>
    

    Now we can see that lda.matrix is needed, so again using one of two functions:

    function (x, grouping, ..., subset, na.action) 
    {
        if (!missing(subset)) {
            x <- x[subset, , drop = FALSE]
            grouping <- grouping[subset]
        }
        if (!missing(na.action)) {
            dfr <- na.action(structure(list(g = grouping, x = x), 
                class = "data.frame"))
            grouping <- dfr$g
            x <- dfr$x
        }
        res <- lda.default(x, grouping, ...)
        cl <- match.call()
        cl[[1L]] <- as.name("lda")
        res$call <- cl
        res
    }
    <bytecode: 0x067bf7b8>
    <environment: namespace:MASS>
    

    And finally here we find a call of na.action which is what we expected. Now this is a function which replaces NA values with column means:

    na.impute <- function (object) {
      temp <- object$x
      k <- which(is.na(temp), arr.ind = TRUE)
      temp[k] <- colMeans(temp, na.rm = TRUE)[k[, 2]]
      structure(list(g = object$g, x = as.matrix(temp)), class = "data.frame")
    }
    lda(my.dat[,-1], my.dat[,1], na.action=na.impute)
    # Call:
    # lda(my.dat[, -1], my.dat[, 1], na.action = na.impute)
    #
    # Prior probabilities of groups:
    #   0   1 
    # 0.5 0.5 
    #
    # Group means:
    #         V2        V3
    # 0 6.133333 -2.366667
    # 1 2.666667 -1.266667
    #
    # Coefficients of linear discriminants:
    #           LD1
    # V2 -0.8155124
    # V3 -1.1614265
    

    Now considering predict and na.action it is unavailable option: see getAnywhere("predict.lda"), there is no usage of this argument.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
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 used javascript for loading a picture on my website depending on which small

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.