R: qqplot using cauchy - r

I have asked this question elsewhere
I want to verify if my data follows a normal or any other type of distribution (like cauchy for example).
I really want to understand how to use qqplot =]
Even though the qqnorm works well:
qqnorm(data);qqline(data)
When I try the qqplot:
qqplot(data, "normal")
qqplot(data, "cauchy")
it generates an error:
Error in plot.window(...) : valores finitos são necessários para 'ylim'
In addition it creates the 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

You should read the documentation for qqplot. The second argument to qqplot should be another data vector, not a string. If you want to compare your data to a specific distribution, you can follow the technique used in qqnorm and generate a vector of quantiles for any distribution. Let's say x is the data we want to plot:
x <- rcauchy(5000)
Since x has 5000 elements, we want to generate 5000 evenly-spaced quantiles from our target distribution. First, let's try the normal distribution:
y.norm <- qnorm(ppoints(length(x)))
qqplot(x, y.norm)
Now let's try the same thing with the Cauchy distribution.
y.cauchy <- qcauchy(ppoints(length(x)))
qqplot(x, y.cauchy)
(Note that the Cauchy distribution in particular will not behave very well in QQ plots, so this may not actually help you with your real goal.)

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)

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)

Will only plot Factors?

Im working in my new Data set and I always start it with
options(StringsAsFactors = FALSE)
The problem im having now, is that R will only plot the Data I set if the strings as factors options is set to TRUE.
Whenever I try to plot with Stringsasfactors = FALSE it will give me the next Error message.
plot(Data$Jobs, Data$RXH)
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
But when I set Stringsasfactors TRUE it plots it without problem...
This is the script.
#Setting WD.
getwd()
setwd("C:/Windows/System32/config/systemprofile/Documents/R proj")
options(stringsAsFactors = F)
get <- read.csv("WorkExcelR.csv", header = TRUE, sep = ",")
Data <- na.omit(get)
And this is Data$Jobs and Data$RXH
> Data$Jobs
[1] "Playstation" "RWC Heineken" "Jagermeister" "RWC Heineken"
[5] "RWC Heineken" "RWC Heineken"
> Data$RXH
[1] 90 90 100 90 90 90
The problem you are illustrating stems from the fact that there is a plot.factor function but no plot.character function. You can see the available plot.-methods by typing:
methods(plot)
This is not particularly well described in the help page for ?plot, but there is a separate help page for ?plot.factor. Functions in R are dispatched on the basis of their arguments: S3 functions on the basis only of the class of their first argument and S4 methods on the basis of their argument signatures. In a sense the plot.factor function elaborates on that strategy, because it then dispatches to different plotting routines based on the second argument's class as well, assuming it is matched by position or named y.
You have a couple of choices: Force the plot method which then needs to be caled using the ::: infix function since plot.factor is not exported or do the coercion yourself or call a more specific plotting type.
graphics:::plot.factor(Data$Jobs, Dat
plot(factor(Data$Jobs), Data$RXH)
boxplot(Data$RXH ~Data$Jobs) # which is the result if x is factor and y is numeric

Screeplot in R with psych package

I have computed a PCA with the principal function in the psych package in R. I would like to build a screeplot from the eigenvalues, but both scree(PCA) and screeplot(PCA) give me errors and no plot. Is there a function within this package that I'm not aware of (I have very, very little R experience)??
NOTE: I've been simply working in the command line.
Error for scree(PCA):
Error in if (nvar != dim(rx)[1]) { : argument is of length zero
Error for screeplot(PCA):
Error in plot.window(xlim, ylim, log = log, ...) :
need finite 'xlim' values
In addition: Warning messages:
1: In min(w.l) : no non-missing arguments to min; returning Inf
2: In max(w.r) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
Without data it is hard for us to check this. The error message looks like the data is empty.
Here are some tips for R beginners.
Try get help on scree function. Are you missing a parameter? Type in command line.
help(scree)
Look at your variable PCA
head(PCA) - shows first few rows of your data
str(PCA) - shows structure of the variable. Is it what scree function is expecting?
Do you have missing values or text values in your data? The function may be thrown out by these. You can drop missing data - take a look at complete.cases. is.na() is how you check for NA values (i.e. if I wanted to check for NAs in variable mydata, sum(is.na(mydata)) would tell me how many I have. Drop those rows and see if that gets your scree function working okay.
Take a look at the vignette for the package:
https://cran.r-project.org/web/packages/psych/vignettes/overview.pdf
Hope this gets you on track.
Did you enter a correlation matrix as your input to the scree( ) function?
Using my own data, I was able to generate a scree plot with the following two lines of code:
humor_cor <- cor(humor, use = "pairwise.complete.obs")
scree(humor_cor, factors = FALSE)

Basic hexbin with R?

I have results from a survey. I am trying to create a graphic displaying the relationship of two variables: "Q1" and "Q9.1". "Q1" is the independent and "Q9.1" is the dependent. Both variables have responses from like scale questions: -2,-1,0,1,2. A typical plot places the answers on top of each other - not very interesting or informative. I was thinking that hexbin would be the way to go. The data is in lpp.
I have not been able to use "Q1" and "Q9.1" for x and y. However:
> is.numeric("Q1")
[1] FALSE
q1.num <- as.numeric("Q1")
Warning message:
NAs introduced by coercion
The values for Q1 are (hundreds of instances of): -2,-1,0,1,2
How can I make a hexbin graph with this data?
Is there another graph I should consider?
Error messages so far:
Warning messages:
1: In xy.coords(x, y, xl, yl) : NAs introduced by coercion
2: In xy.coords(x, y, xl, yl) : 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
How about taking a slightly different approach? How about thinking of your responses as factors rather than numbers? You could use something like this, then, to get a potentially useful representation of your data:
# Simulate data for testing purposes
q1 = sample(c(-2,-1,0,1,2),100,replace=TRUE)
q9 = sample(c(-2,-1,0,1,2),100,replace=TRUE)
dat = data.frame(q1=factor(q1),q9=factor(q9))
library(ggplot2)
# generate stacked barchart
ggplot(dat,aes(q1,fill=q9)) + geom_bar()
You may want to switch q1 and q9 above, depending on the view of the data that you want.
Perhaps ggplot2's stat_binhex could sort that one for you?
Also, I find scale_alpha useful for dealing with overplotting.

Resources