Plot graphs in R software - r

In using R software I face the error term which is:
dmodel1=list()
for(i in 1:12){
sun.st = i
data1 = fdata(file1, nd = nd, sun.st)
d1= ffit(data1,order=2)
dmodel1[[i]]=d1
fplot(d1, plot.year,label=colnames(data1)[2],ylab=1)
cat(colnames(data1)[2], "\n")
}
the error is:
Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : - not meaningful for factors
3: In Ops.factor(x[, 2], dataff1f) : - not meaningful for factors
4: In max(dataFF2[, 3], na.rm = TRUE) :
no non-missing arguments to max; returning -Inf
So, would you please help me to overcome this?
Thanks
Bah

Can you reproduce the error if you use some random values in your variables? Without the data is difficult to assess whether the data is somehow "corrupted" (or at least inadequate for what you want to do).

Related

Plotting data in R: Error in plot.window(...) : need finite 'xlim' values

Trying to plot some data in R - I am a basic user and teaching myself. However, whenever I try to plot, it fails, and I am not sure why.
> View(Pokemon_BST)
> Pokemon_BST <- read.csv("~/Documents/Pokemon/Pokemon_BST.csv")
> View(Pokemon_BST)
> plot("Type_ID", "Gender_ID")
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
5: In min(x) : no non-missing arguments to min; returning Inf
6: In max(x) : no non-missing arguments to max; returning -Inf
This is my code, but I thought it might be an issue with my .csv file? I have attributed numbers to the "Type_ID" and "Gender_ID" columns. Type_ID has values between 1-20; Gender_ID has 1 for male, 2 for female, and 3 for both. I should state that both ID columns are just made of numeric values. Nothing more.
I then tried using barplot function. This error occurred:
> barplot("Gender_ID", "Type_ID")
Error in width/2 : non-numeric argument to binary operator
In addition: Warning message:
In mean.default(width) : argument is not numeric or logical: returning NA
There are no missing values, no characters within these columns, nothing that SHOULD cause an error according to my basic knowledge. I am just not sure what is going wrong.
To me it seems as you are giving the plot function the wrong inputs.
For the x and y axis plot expects numeric values and you are only providing a single string. The function does not know that the "Type_ID" and "Gender_ID" come from the Pokemon_BST data frame.
To reach your data you must tell R where the object comes from. You do this by opening square brackets behind the object you want to access and write the names of the objects to be accessed into it.
View(Pokemon_BST)
Pokemon_BST <- read.csv("~/Documents/Pokemon/Pokemon_BST.csv")
# Refer to the object
plot(Pokemon_BST["Type_ID"], Pokemon_BST["Gender_ID"])
# Sould also work now
barplot(Pokemon_BST["Gender_ID"], Pokemon_BST["Type_ID"])
See also here for a introduction on subsetting in R
The problem is how you're passing the values to the plot function. In your code above, "Gender_ID" is just some string and the plot function doesn't know what to do with that. One way to plot your values is to pass the vectors Pokemon_BST$Gender_ID and Pokemon_BST$Type_ID to the function.
Here's a sample dataframe with the plot you were intending.
Pokemon_BST <- data.frame(
Type_ID = sample(1:20, 10, replace = TRUE),
Gender_ID = sample(1:3, 10, replace = TRUE))
plot(Pokemon_BST$Gender_ID, Pokemon_BST$Type_ID)

polycor package - hetcor error in optim

I'm trying to run a factor analysis on a set of 80 dichotomous variables (1440 cases) using the hector function from the polycor package and the instructions I found here: http://researchsupport.unt.edu/class/Jon/Benchmarks/BinaryFA_L_JDS_Sep2014.pdf
Sadly, after I select just the variables interest from the rest of my dataset and run the factor analysis on them, I seem to consistently get the following error and warnings
Error in optim(0, f, control = control, hessian = TRUE, method = "BFGS") :
non-finite finite-difference value [1]
In addition: Warning messages:
1: In log(P) : NaNs produced
2: In log(P) : NaNs produced
This is with the command/when I hit the step described in the above PDF:
testMat <- hetcor(data)$cor
No idea what this means or how to proceed... Your thoughts are appreciated. Thank you!

marginalModelPlots error "need finite 'xlim' values"

I'm trying to run some diagnostics on a binary logistic regression model. Specifically, the marginal model plots. Unfortunately, I keep getting the "need finite 'xlim' values" error. The code below reproduces the issue. My model includes both numeric and categorical variables (which get converted to dummy variables in the model). Anyway, I know this error can occur when all values are NA, but that isn't the case for any of my data and I'm not sure whats going on.
set.seed(020275)
df <- data.frame(y=sample(c(0,1), 10, replace=TRUE),
cat=sample(c("Red", "Blue", "Green"), 10, replace=TRUE),
loc=sample(c("North", "South", "East", "West"), 10, replace=TRUE),
count=runif(10, 0, 10),
stringsAsFactors = FALSE)
glmModel <- glm(y ~ cat + loc + count, family=binomial(), data=df)
glmModel
library(car)
marginalModelPlots(glmModel)
I get the following error:
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
Looking for some ideas/suggestions/guidance on how to deal with this.
It appears the character data typed vectors (cat and loc in the example above) are not compatible with marginalModelPlots, at least for the version of the car package I'm currently using (2.1-1). I found I could use the terms parameter to limit the plots to a subset of the variables while also including the Linear Predictor plot (as shown below).
marginalModelPlots(glmModel, terms= ~ count)

R software, making boxplot, need finite 'ylim' value

> B<-subset(olympic,sport=="basketball")
> BM<-subset(B,sex=="M"
+ )
> boxplot(BM$height)
Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs):
need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
This is what happened when I try to plot the graph. I'm new to R.
Just add ylim=c(0,300)) to your code
I see nothing wrong in the command though it can be shortened to:
> BM <- subset(olympic,sport=="basketball" & sex == 'M')
> boxplot(BM$height)
The error that you are getting might be because of the fact that data.frame BM has zero rows.
I would recommend you to please check the case of values for sport (i.e. whether in the dataset, 'Basketball' is present and you are searching for 'basketball')

Plotting C5.0 Tree in R

I am trying to plot a C5.0 object tree in R but it is giving the following error and I can't seem to find out how to fix it.
plot(model)
Error in partysplit(varid = as.integer(i), index = index, info = k, prob = NULL) :
minimum of ‘index’ is not equal to 1
In addition: Warning message:
In min(index, na.rm = TRUE) :
no non-missing arguments to min; returning Inf
It seems that the factors in your data frame contain spaces. I was facing the same issue, then I removed spaces from them and now it works.
for example, if a variable has factors " bad" and " good" then change them to "bad" and "good".
"The error itself is due to NA values being passed in the index vector. The root cause is probably that the factor levels are being split on spaces" Found here https://github.com/topepo/C5.0/issues/10
try this
library(rattle)
fancyRpartPlot(model)

Resources