• Register
0 votes
180 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

6 5 3
7,540 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
9 7 4
38,600 points

Related questions

1 vote
1 answer 598 views
598 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
1 vote
1 answer 1.8K views
1.8K 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 1.2K views
1.2K 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
0 votes
1 answer 2.8K views
2.8K views
Problem : While I am trying to run the boxcox transformation with the following code urban1 <- subset(ski,urban <= 4,na.rm=TRUE) ski$gender <- as.numeric((as.character(ski$gender)),na.rm=TRUE) urban1 <- as.numeric((as.character(urban1))) a <- (ski$gender*urban1) ... -> boxcar -> boxcar.formula -> lm -> lm.fit Execution halted Please let me know if anyone knows the solution for this error message.
asked Nov 27, 2019 alecxe 7.5k points
0 votes
1 answer 459 views
459 views
Problem: I have only the fundamental knowledge related to R. I want to simulate the very basic moving average process in R which is as shown below for your reference: x_t = Z_(t-1)-0.4Z_(t-2)+Z_t. Please find below my code in R: z = rnorm(500, 0, 1) x ... was unable to find the exact solution for my above error. I am looking for some help from R expert if possible to fix my above R related error.
asked Aug 12, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 1.3K views
1.3K views
Problem : I want to use the glmnet package on a dataset. I am trying to use cv.glmnet() to get the lambda value for the glmnet(). Please find below the dataset and error message: > head(t2) X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 1 ... in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix Any suggestions regarding the solution?
asked Dec 14, 2019 alecxe 7.5k points
1 vote
1 answer 156 views
156 views
Problem: I have very fundamental knowledge of logistic regression. I am trying to write code on logistic regression. I have done a lot of research on this issue but because of my limited knowledge I am still unable to fix the below error. Error ... (data) stock.direction <- data$Direction training_model=glm(stock.direction~data$lag2,data=data,family=binomial) Please help me in fixing above error.
asked May 26, 2020 Martin K 6.6k points
0 votes
2 answers 1.1K views
1.1K views
Problem : I am new to RandomForest model. While predicting my test data using the RandomForest model I am often facing below ValueError. &ldquo;Input contains nan, infinity or a value too large for dtype('float64')&rdquo; I have spent more than two days on the above error but I am unable to fix above error. Can somebody help me in fixing above error?
asked Feb 24, 2020 mphil 2.3k points
0 votes
1 answer 219 views
219 views
How to convert non-finite calues to integer?
asked Sep 22, 2020 Daniel Anderson 4k points
1 vote
2 answers 219 views
219 views
Problem : I am very new to R. Currently I want to split the column of my data frame (menuA) is as shown below Column1 1|3|4|5 4|5|6|7 I was trying to split my numbers in above columnA so I coded it as shown below. menuA <- data.frame (do.call('rbind', ... 120"),  :   number of columns of result is not a multiple of vector length (arg 1) Please let me know if all my data is separated properly?
asked Apr 22, 2020 stewart 4k points