• Register
0 votes
314 views

Problem :

I have done a lot of research on this extensively without finding any solution on it. I have tried cleaning my data set as follows:

library("myraster")
impute.mean <- function(l) replace(l, is.na(l) | is.nan(l) | is.infinite(l) , 
mean(l, na.rm = TRUE))
losses <- apply(losses, 2, impute.mean)
colSums(is.na(losses))
isinf <- function(l) (NA <- is.infinite(l))
infout <- apply(losses, 2, is.infinite)
colSums(infout)
isnan <- function(l) (NA <- is.nan(l))
nanout <- apply(losses, 2, is.nan)
colSums(nanout)

But the problem arises while running the predict algorithm:

options(warn=2)
p  <-   predict(default.rf, losses, type="prob", inf.rm = TRUE, na.rm=TRUE, nan.rm=TRUE)

All my research says it should be NA's or Inf's or NaN's in the data but I don't have any

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I guess you are failing to replace Inf values with the means of column vectors. This is happening because of the na.rm = TRUE argument in your call to mean() within the impute.mean function which does exactly what it says i.e. it removes NA values and clearly not Inf ones

You can cross check it, by following code example:

impute.mean <- function(l) replace(l, is.na(l) | is.nan(l) | is.infinite(l), mean(l, na.rm = TRUE))
losses <- apply(losses, 2, impute.mean)
sum( apply( losses, 2, function(.) sum(is.infinite(.))) )
# [1] 696

To remove the infinite values, please use following:

impute.mean <- function(l) replace(l, is.na(l) | is.nan(l) | is.infinite(l), mean(l[!is.na(l) & !is.nan(l) & !is.infinite(l)]))
losses <- apply(losses, 2, impute.mean)
sum(apply( losses, 2, function(.) sum(is.infinite(.)) ))
# [1] 0
38.6k points

Related questions

0 votes
1 answer 73 views
73 views
Problem: Ok, I am getting the error "Error in randomForest.default(m, y, ...) : Can't have empty classes in y." When running Randomforest in my code.
asked May 1 Supti 13k points
1 vote
1 answer 1.4k views
1.4k views
Problem : Currently I am learning r. I have the data table "normal" which has the numeric as far as I know normalized values. If I try to execute the below command k <- kmeans(normal,center=3) Then I always face the below error: &ldquo;Error in do_one(nmeth) : NA/NaN/ ... call (arg 1)&rdquo; I am trying to fix it by researching on it but still unable to fix it. Can somebody help me in fixing it?
asked Mar 28, 2020 morrisBson 3.2k points
0 votes
1 answer 79 views
79 views
Problem: I am really new to RStudio and desperately trying to get to grips with it all, so I apologise if this is a silly question. I am trying to generate a heatmap for my RNA Seq Data set using pheatmap, however I am coming up again a number of errors. My data looks like this: ... 2.32 3.45 3.74 ... $ p_value : num 0.00045 0.0006 0.0006 0.00005 Thank you and I look forward to your responses.
asked May 2 Yeamin 23.5k points
0 votes
1 answer 13 views
13 views
Problem: Na/nan/inf in foreign function call (arg 6)
asked Jul 13 Sawyer Parviz 168k points
0 votes
1 answer 17 views
17 views
Problem: Na/nan/inf in foreign function call (arg 11).
asked Jul 10 Rada Gracelynn 104k points
0 votes
0 answers 14 views
14 views
Error in knn(faktor1, joni, target, k = 1, prob = TRUE) : NA/NaN/Inf in foreign function call (arg 6) please help me..
asked Apr 24 robian23 120 points
0 votes
1 answer 177 views
177 views
Problem: This is my problem: Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, : na/nan/inf in 'y' Any help.
asked Mar 13 Wafa Abu Yousef 6.1k points
1 vote
1 answer 3.5k views
3.5k views
Problem : I want to perform a logistic regression but I am facing following error I am unable to find my mistake. summary(glm(prefmerkel~angst+crisismerkel+leadership+trustworthiness+ideology+pid+agegroups+gender+region,data=gles)) Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, ... In Ops.factor(eta, offset) : -' nicht sinnvoll für Faktoren 3: In Ops.factor(y, mu) : -' nicht sinnvoll für Faktoren
asked Nov 24, 2019 alecxe 7.5k points
0 votes
1 answer 10 views
10 views
Problem: R numeric 'envir' arg not of length one
asked Jul 20 asha 121k points
0 votes
1 answer 1.7k views
1.7k views
Problem : Suppose I have data.frame a And I am using following code m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) Here the col2 has some NA values And col3,col4 have values less than 1. I am getting following error: Error in lm.fit ... because of the NAs. Problem with this warning is interpreted by every google results is pointing towards the NA values. Have I misinterpreted the error ?
asked Nov 22, 2019 peterlaw 6.9k points