I was trying to do a partition plot, and I used the following codes:
install.packages('klaR')
library(klaR)
partimat(Type~. , data = training, method = "lda")
partimat('Type'~. , data = training, method = "qda")
R gave me this error code:
Error in `[.data.frame`(m, xvars) : undefined columns selected
and my data is like this
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 178 obs. of 13 variables:
$ Alcohol : num 14.2 13.2 13.2 14.4 13.2 ...
$ Malic acid : num 1.71 1.78 2.36 1.95 2.59 1.76 1.87 2.15 1.64 1.35 ...
$ Ash : num 2.43 2.14 2.67 2.5 2.87 2.45 2.45 2.61 2.17 2.27 ...
$ Alcalinity of ash : num 15.6 11.2 18.6 16.8 21 15.2 14.6 17.6 14 16 ...
$ Magnesium : int 127 100 101 113 118 112 96 121 97 98 ...
$ Total phenols : num 2.8 2.65 2.8 3.85 2.8 3.27 2.5 2.6 2.8 2.98 ...
$ Flavanoids : num 3.06 2.76 3.24 3.49 2.69 3.39 2.52 2.51 2.98 3.15 ...
$ Nonflavanoid phenols: num 0.28 0.26 0.3 0.24 0.39 0.34 0.3 0.31 0.29 0.22 ...
$ Proanthocyanins : num 2.29 1.28 2.81 2.18 1.82 1.97 1.98 1.25 1.98 1.85 ...
$ Color intensity : num 5.64 4.38 5.68 7.8 4.32 6.75 5.25 5.05 5.2 7.22 ...
$ Hue : num 1.04 1.05 1.03 0.86 1.04 1.05 1.02 1.06 1.08 1.01 ...
$ Proline : int 1065 1050 1185 1480 735 1450 1290 1295 1045 1045 ...
$ Type : int 1 1 1 1 1 1 1 1 1 1 ...
Please let me know how to solve it!
There is no Type variable in the UCI Machine Learning Wine data set. The classification variable is class, and it is the first column in the data set.
# data source: UCI ML Repository Wine data
# https://archive.ics.uci.edu/ml/datasets/wine
library(klaR)
colNames <- c("class","alcohol","malicAcid","ash","acalinityOfAsh",
"magnesium","totalPhenols","flavanoids","nonflavanoidPhenols",
"proanthocyanins","colorIntensity","hue","od280.od315OfDilutedWines",
"proline")
wine <- read.csv("./data/wine.csv",header=FALSE,col.names=colNames)
wine$class <- as.factor(wine$class)
partimat(class ~ alcohol + malicAcid, data=wine, method="lda",plot.matrix=FALSE)
...and the output:
I had the same problem and I could fix it by changing the name of my varibles. In my data set I had a variable whose name had a blank space at the beginning. The program could not recognize it and that triggered the error. I removed that blank space and the problem disappeared.
Related
I am trying to run a shapiro-wilk normality test on R (Rcmdr to be more accurate) by going to "Statistics=>Summary=>Descriptive statistics" and then selecting one of my dependent variable and choosing "summary by group".
Rcmdr automatically triggers the following code :
normalityTest(Algometre.J0 ~ Modalite, test="shapiro.test",
data=Dataset)
And I am getting the following error message :
'groups' must be a factor.
I have already categorized my independant variable as a factor (I swear, I did !)
Any idea what's wrong ?
Thanx in advance
Here is what str(Dataset) shows :
'data.frame': 76 obs. of 11 variables:
$ Modalite : chr "C" "C" "C" "C" ...
$ Angle.J0 : num 20.1 20.5 21 22.5 19.1 ...
$ Angle.J1 : num 21.7 22.6 22.8 23.3 20.5 ...
$ Angle.J2 : num 22.3 23 23.9 24.2 21 ...
$ Epaisseur.J0: num 1.97 1.54 1.76 1.89 1.53 1.87 1.54 2 1.79 1.41 ...
$ Epaisseur.J1: num 2.07 1.49 1.87 1.91 1.54 1.9 1.51 2.03 1.71 1.48 ...
$ Epaisseur.J2: num 2.08 1.69 1.77 2 1.61 1.99 1.38 2.06 1.86 1.53 ...
$ Algometre.J0: num 45 40 105 165 66.3 ...
$ Algometre.J1: num 32.7 39.7 91.7 124 63.7 ...
$ Algometre.J2: num 51.3 58.7 101 138 60.3 ...
$ ObsNumber : int 1 2 3 4 5 6 7 8 9 10 ...
What does that mean ?
I have a list of data frames, for example the first three
[[1]]
01oct 24sep 17sep 10sep 03sep 27aug 20aug 13aug 06aug 30jul 23jul 16jul 09jul 02jul 25jun 18jun 11jun 04jun 28may 21may 14may 07may 30apr 23apr
3.25 9.50 0.80 6.85 6.70 6.65 14.35 62.35 9.75 2.35 18.55 8.90 17.85 14.75 0.90 0.50 17.05 19.15 44.25 0.15 42.05 10.45 12.00 5.05
16apr 09apr 02apr
0.15 12.90 23.20
[[2]]
30sep 23sep 16sep 09sep 02sep 26aug 19aug 12aug 05aug 29jul 22jul 15jul 08jul 01jul 24jun 17jun 10jun 03jun 27may 20may 13may 06may 29apr 22apr
1.90 4.60 23.95 3.95 12.65 26.30 38.30 2.80 2.35 34.10 8.30 7.30 28.85 2.45 5.20 15.35 1.85 36.75 0.95 8.40 22.35 37.70 6.00 0.40
15apr 08apr
3.25 5.45
[[3]]
28sep 21sep 14sep 07sep 31aug 24aug 17aug 10aug 03aug 27jul 20jul 13jul 06jul 29jun 22jun 15jun 08jun 01jun 25may 18may 11may 04may 27apr 20apr
5.85 13.70 2.85 12.50 43.40 13.25 5.65 4.80 12.20 5.40 3.05 12.90 20.70 21.75 13.20 18.60 0.70 13.15 20.30 2.40 2.30 13.50 4.70 19.60
13apr 06apr
17.60 14.50
I am trying to create vectors of the corresponding elements of each data frame. In the above example, the first three elements of my first vector would be 3.25, 1.90, 5.85. The second vector would be 9.5, 4.6, 13.7. The strings showing dates ideally would be left out, since at a later stage the vectors will be used to compute correlations.
My ultimate goal is an array of these vectors.
I know this could be done with a nested loops, however I've tried and have other problems with this kind of array assignment in R (but that's for another thread). I also know that nested loops are inefficient and not best practice (at least I understood that).
What is the most reproducible way to construct these vectors and the array of them in R?
library(tidyverse)
# example data
l <- list(
data.frame(a = 3.25, b = 9.5),
data.frame(c = 1.90, d = 4.6),
data.frame(e = 5.85, d = 13.70)
)
combined <-
l %>%
map(~ {
# unify column names
colnames(.x) <- colnames(.x) %>% length() %>% seq()
.x
}) %>%
reduce(bind_rows)
combined[[1]]
#> [1] 3.25 1.90 5.85
combined[[2]]
#> [1] 9.5 4.6 13.7
Created on 2022-03-01 by the reprex package (v2.0.0)
It sounds like you want a transposition of the vectors. This is going to run into problems, because to do so suggests that the vectors should all be the same length, which they're not.
lengths(yourlist)
# [1] 27 26 26
We can fix that by padding them with NA,
yourlist <- lapply(yourlist, `length<-`, max(lengths(yourlist)))
yourlist
# [[1]]
# 01oct 24sep 17sep 10sep 03sep 27aug 20aug 13aug 06aug 30jul 23jul 16jul 09jul 02jul 25jun 18jun 11jun 04jun 28may 21may 14may 07may 30apr 23apr 16apr 09apr 02apr
# 3.25 9.50 0.80 6.85 6.70 6.65 14.35 62.35 9.75 2.35 18.55 8.90 17.85 14.75 0.90 0.50 17.05 19.15 44.25 0.15 42.05 10.45 12.00 5.05 0.15 12.90 23.20
# [[2]]
# 30sep 23sep 16sep 09sep 02sep 26aug 19aug 12aug 05aug 29jul 22jul 15jul 08jul 01jul 24jun 17jun 10jun 03jun 27may 20may 13may 06may 29apr 22apr 15apr 08apr
# 1.90 4.60 23.95 3.95 12.65 26.30 38.30 2.80 2.35 34.10 8.30 7.30 28.85 2.45 5.20 15.35 1.85 36.75 0.95 8.40 22.35 37.70 6.00 0.40 3.25 5.45 NA
# [[3]]
# 28sep 21sep 14sep 07sep 31aug 24aug 17aug 10aug 03aug 27jul 20jul 13jul 06jul 29jun 22jun 15jun 08jun 01jun 25may 18may 11may 04may 27apr 20apr 13apr 06apr
# 5.85 13.70 2.85 12.50 43.40 13.25 5.65 4.80 12.20 5.40 3.05 12.90 20.70 21.75 13.20 18.60 0.70 13.15 20.30 2.40 2.30 13.50 4.70 19.60 17.60 14.50 NA
Given that, a list-transpose:
yourlist2 <- do.call(Map, c(list(f = c, use.names = FALSE), yourlist))
str(yourlist2)
# List of 27
# $ : num [1:3] 3.25 1.9 5.85
# $ : num [1:3] 9.5 4.6 13.7
# $ : num [1:3] 0.8 23.95 2.85
# $ : num [1:3] 6.85 3.95 12.5
# $ : num [1:3] 6.7 12.6 43.4
# $ : num [1:3] 6.65 26.3 13.25
# $ : num [1:3] 14.35 38.3 5.65
# $ : num [1:3] 62.4 2.8 4.8
# $ : num [1:3] 9.75 2.35 12.2
# $ : num [1:3] 2.35 34.1 5.4
# $ : num [1:3] 18.55 8.3 3.05
# $ : num [1:3] 8.9 7.3 12.9
# $ : num [1:3] 17.9 28.9 20.7
# $ : num [1:3] 14.75 2.45 21.75
# $ : num [1:3] 0.9 5.2 13.2
# $ : num [1:3] 0.5 15.3 18.6
# $ : num [1:3] 17.05 1.85 0.7
# $ : num [1:3] 19.1 36.8 13.2
# $ : num [1:3] 44.25 0.95 20.3
# $ : num [1:3] 0.15 8.4 2.4
# $ : num [1:3] 42 22.4 2.3
# $ : num [1:3] 10.4 37.7 13.5
# $ : num [1:3] 12 6 4.7
# $ : num [1:3] 5.05 0.4 19.6
# $ : num [1:3] 0.15 3.25 17.6
# $ : num [1:3] 12.9 5.45 14.5
# $ : num [1:3] 23.2 NA NA
I have the following data set (which I import from a 6-column Excel file with a .csv file extension):
treas <- read.csv(file = 'treas.csv', header = TRUE, stringsAsFactors = FALSE)
2YR 3YR 5YR 7YR 10YR 30YR
0.41 0.85 1.65 2.18 2.6 3.43
0.41 0.85 1.65 2.2 2.61 3.45
0.4 0.82 1.63 2.17 2.59 3.44
0.41 0.86 1.66 2.19 2.6 3.44
0.43 0.88 1.69 2.22 2.62 3.45
0.45 0.93 1.71 2.24 2.64 3.47
0.44 0.91 1.7 2.23 2.65 3.47
0.42 0.88 1.66 2.17 2.58 3.41
0.45 0.93 1.7 2.21 2.6 3.41
0.49 0.95 1.71 2.21 2.61 3.4
0.51 0.99 1.77 2.27 2.66 3.44
0.48 0.95 1.71 2.21 2.61 3.43
0.48 0.94 1.71 2.22 2.64 3.47
0.5 0.94 1.71 2.22 2.63 3.44
0.48 0.96 1.72 2.23 2.63 3.45
0.49 0.95 1.7 2.19 2.59 3.41
0.48 0.92 1.68 2.17 2.57 3.38
0.46 0.9 1.64 2.14 2.53 3.35
0.45 0.88 1.64 2.14 2.54 3.36
0.47 0.88 1.62 2.13 2.53 3.34
0.47 0.9 1.66 2.17 2.58 3.4
0.49 0.95 1.71 2.22 2.64 3.46
0.52 0.98 1.74 2.25 2.65 3.47
0.52 1 1.74 2.24 2.63 3.44
0.51 0.99 1.7 2.19 2.58 3.38
0.51 0.97 1.68 2.17 2.57 3.37
0.46 0.93 1.66 2.15 2.55 3.38
0.48 0.92 1.65 2.13 2.53 3.34
0.48 0.95 1.68 2.17 2.55 3.36
When I call the cov() method on the the treas data frame, I see the following error message:
Error: is.numeric(x) || is.logical(x) is not TRUE
To check the data types, I use:
sapply(treas, typeof)
The result is:
2YR 3YR 5YR 7YR 10YR 30YR
"character" "character" "character" "character" "character" "character"
Calling str(treas) reveals:
str(treas)
'data.frame': 1252 obs. of 6 variables:
$ 2YR : Factor w/ 235 levels ".","0.34","0.35",..: 8 8 7 8 10 12 11 9 12 16 ...
$ 3YR : chr w/ 219 levels ".","0.66","0.69",..: 18 18 15 19 21 26 24 21 26 28 ...
$ 5YR : chr w/ 207 levels ".","0.94","0.95",..: 67 67 65 68 71 73 72 68 72 73 ...
$ 7YR : chr w/ 192 levels ".","1.19","1.20",..: 96 98 95 97 100 102 101 95 99 99 ...
$ 10YR : chr w/ 178 levels ".","1.37","1.38",..: 118 119 117 118 120 122 123 116 118 119 ...
$ 30YR : chr w/ 125 levels ".","2.11","2.14",..: 121 123 122 122 123 125 125 120 120 119 ...
I've tried to force the data frame to numeric using:
lapply(treas, as.numeric)
But, doing so results in:
Warning messages:
1: In lapply(treas, as.numeric) : NAs introduced by coercion
2: In lapply(treas, as.numeric) : NAs introduced by coercion
3: In lapply(treas, as.numeric) : NAs introduced by coercion
4: In lapply(treas, as.numeric) : NAs introduced by coercion
5: In lapply(treas, as.numeric) : NAs introduced by coercion
6: In lapply(treas, as.numeric) : NAs introduced by coercion
Then, I still get the same error when calling cov(treas):
Error: is.numeric(x) || is.logical(x) is not TRUE
Anyone see what I'm doing incorrectly here? Thanks!
I am new to R.
The daily data of a pump has been taken. The data is of two years and has the row of 742 numbers.
cw<-read.csv("RCW1.csv")
str(cw)
data.frame': 742 obs. of 14 variables:
$ date : Date, format: "2016-04-01" "2016-04-02" "2016-04-03" ...
$ amp : num 226 227 0 225 226 ...
$ brg_de_tmp : num 38.1 38.1 39.6 41.6 41.5 ...
$ brg_nde_tmp: num 78.6 79.1 72 79.9 80.4 ...
$ kg : num 2.07 2.07 0.06 2.29 2.28 2.3 2.11 2.1 2.11 2.11 ...
$ level1 : num 8.45 8.46 8.69 8.67 8.43 8.6 8.39 8.5 8.46 8.65 ...
$ level2 : num 8.44 8.46 8.67 8.65 8.42 8.59 8.38 8.48 8.46 8.63 ...
$ mde_xvib : num 1.15 1.35 0.28 1.05 1.15 1.06 1.25 1.25 1.25 1.25 ...
$ mde_zvib : num 1.37 1.57 0.4 1.18 1.13 1.38 1.28 1.57 1.3 1.5 ...
$ rpm : num 296.46 296.91 -4.76 297.09 297.91 ...
$ mde_yvib : num 2.09 2.38 0.34 2 1.82 2.24 2.17 2.56 1.9 2.27 ...
$ m_nde_yvib : num 1.15 1.13 0.35 0.96 0.96 0.96 1.15 1.06 1.15 1.15 ...
$ m_nde_zvib : num 1.53 1.63 0.27 1.33 1.43 1.4 1.76 1.63 1.79 1.71 ...
$ permit : chr "#N/A" "#N/A" "CW Pump house: Motor stand" "#N/A" ...
convert it into time series
cw_x <- xts(cw, order.by=as.Date(cw[,1], "%Y/%m/%d"))
cw_ts<-as.ts(cw_x)
> head(cw_ts)
Time Series:
Start = 1
End = 6
Frequency = 1
date amp brg_de_tmp brg_nde_tmp kg level1 level2 mde_xvib mde_zvib rpm mde_yvib
1 2016-04-01 226.05 38.06 78.61 2.07 8.45 8.44 1.15 1.37 296.46 2.09
2 2016-04-02 226.59 38.08 79.13 2.07 8.46 8.46 1.35 1.57 296.91 2.38
3 2016-04-03 0.00 39.57 71.96 0.06 8.69 8.67 0.28 0.40 -4.76 0.34
4 2016-04-04 225.01 41.57 79.91 2.29 8.67 8.65 1.05 1.18 297.09 2.00
5 2016-04-05 226.41 41.54 80.43 2.28 8.43 8.42 1.15 1.13 297.91 1.82
6 2016-04-06 225.65 41.08 79.89 2.30 8.60 8.59 1.06 1.38 297.55 2.24
m_nde_yvib m_nde_zvib permit
1 1.15 1.53 #N/A
2 1.13 1.63 #N/A
3 0.35 0.27 CW Pump house: Motor stand
4 0.96 1.33 #N/A
5 0.96 1.43 #N/A
6 0.96 1.40 #N/A
I have two questions.
Number 1: How can I get "dates" in X-axis while plotting the following code.
autoplot(cw_ts[,2:5],facets = TRUE) + ylab("parameters")
See I am getting row number in X-axis, I want to change to dates.
Question number 2: I am trying to get seasonal plot with following two codes as describes by Robjhyndman book "Forecasting: principle and practice"
ggseasonplot(cw_ts, year.labels=TRUE, year.labels.left=TRUE) +
ylab("") +
ggtitle("Seasonal plot: Pump parameter")
AND
ggseasonplot(cw_ts, polar=TRUE) +
ylab("") +
ggtitle("Polar seasonal plot: Pump parameter")
So how do i impute my time series into above code. While I am getting following error.
ggseasonplot(cw_ts, year.labels=TRUE, year.labels.left=TRUE) +
+ ylab("") +
+ ggtitle("Seasonal plot: Pump parameter")
Error in ggseasonplot(cw_ts, year.labels = TRUE, year.labels.left = TRUE) :
Data are not seasonal
AND
ggseasonplot(cw_ts, polar=TRUE) +
+ ylab("") +
+ ggtitle("Polar seasonal plot: Pump parameter")
Error in ggseasonplot(cw_ts, polar = TRUE) : Data are not seasonal
>
Any suggestion will help me a lot. Thank You.
I am learning biplot with wine data set. How does R know Barolo, Grignolino and Barbera are wine.class while we don't see the wine class column in the data set?
More details about the wine data set are in the following links
ggbiplot - how not to use the feature vectors in the plot
https://github.com/vqv/ggbiplot
Thanks very much
In the wine dataset, you have 2 objects, one data.frame wine with 178 observations of 13 quantitative variables:
str(wine)
'data.frame': 178 obs. of 13 variables:
$ Alcohol : num 14.2 13.2 13.2 14.4 13.2 ...
$ MalicAcid : num 1.71 1.78 2.36 1.95 2.59 1.76 1.87 2.15 1.64 1.35 ...
$ Ash : num 2.43 2.14 2.67 2.5 2.87 2.45 2.45 2.61 2.17 2.27 ...
$ AlcAsh : num 15.6 11.2 18.6 16.8 21 15.2 14.6 17.6 14 16 ...
$ Mg : int 127 100 101 113 118 112 96 121 97 98 ...
$ Phenols : num 2.8 2.65 2.8 3.85 2.8 3.27 2.5 2.6 2.8 2.98 ...
$ Flav : num 3.06 2.76 3.24 3.49 2.69 3.39 2.52 2.51 2.98 3.15 ...
$ NonFlavPhenols: num 0.28 0.26 0.3 0.24 0.39 0.34 0.3 0.31 0.29 0.22 ...
$ Proa : num 2.29 1.28 2.81 2.18 1.82 1.97 1.98 1.25 1.98 1.85 ...
$ Color : num 5.64 4.38 5.68 7.8 4.32 6.75 5.25 5.05 5.2 7.22 ...
$ Hue : num 1.04 1.05 1.03 0.86 1.04 1.05 1.02 1.06 1.08 1.01 ...
$ OD : num 3.92 3.4 3.17 3.45 2.93 2.85 3.58 3.58 2.85 3.55 ...
$ Proline : int 1065 1050 1185 1480 735 1450 1290 1295 1045 1045 ...
There is also one vector wine.class that contains 178 observations of the qualitative wine.class variable:
str(wine.class)
Factor w/ 3 levels "barolo","grignolino",..: 1 1 1 1 1 1 1 1 1 1 ...
The 13 quantitative variables are used to compute the PCA:
wine.pca <- prcomp(wine, scale. = TRUE)
while the wine.class variable is just used to color the points on the plot