Extraction of specific result in R outputs - r

I want to extract the values of "b1p" and "b2p" from the mardia's command and want to save it in bskew.
For this i have used the "psych" package R version is 4.0.3. I have tried several commands for extraction but failed.
bskew <- mardia$b1p
bskew <- mardia[b1p
bskew <- mardia[[b1p
for this i got the error "object of type 'closure' is not subsettable"
By using names() i got only names and by using class() i got "psych", "mardia".
By using summary() i got the message "Warning message:
In summary.psych(mardia(x)) :
I am sorry, I do not have a summary function for this object" and then i used mna$coefficients[[]] command
and i got the message "NULL".
I saved my mardia command in mna.
Minimum Working Example is:
n0 <- 5
p0 <- 2
m0 <- matrix(rep(0,p0),ncol=p0)
s0 <- diag(1,p0)
x <- rmvnorm(5,mean=m0, sigma=s0)
mardia$"b1p"
bskew <- mardia["b1p"]
bskew <- mardia[["b1p"]]
bkurt <- mardia[["b2p"]]
bskew <- mardia$b1p$
mna<-mardia(x)
class(mna)
names(mna)
summary(mardia(x))
summary(mna)
sk1 <- mna$coefficients[[3]]
mna$coefficients

the error is because you're trying to subset a function mardia which always throws the error you have, also you should subset the mna object instead of subsetting the actual function.
> mna$b1p
[1] 1.95888
> mna["b1p"]
$b1p
[1] 1.95888
> mna[["b1p"]]
[1] 1.95888
> mardia(x)$b1p
[1] 1.95888
> mardia$b1p
Error in mardia$b1p : object of type 'closure' is not subsettable
> mardia<-mardia(x)
> mardia$b1p
[1] 1.95888

Related

R stm package error: "vectorized sources must have a positive length entry"

I think I'm making a pretty simple mistake but I'm a rookie at R and am having a hard time figuring it out. I'm trying to use the 'stm' package in R do some topic-modeling on a dataset of tweets I scraped.
The dataset is formatted in two columns, one with the name of the tweet-sender, with column header "meta" and the other with the vocab of the tweet, column header, "vocab". After running the script below, I get the following errors:
Error: is.Source(s) is not TRUE
In addition: Warning message:
In is.Source(s) : vectorized sources must have a positive length entry
library(stm)
library(igraph)
setwd("c:/Users/Adam/Desktop/RTwitter")
data <-read.csv("TweetDataSTM.csv")
processed <- textProcessor(data$documents, metadata = data)
out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
docs <- out$documents
vocab <- out$vocab
meta <-out$meta
> library(stm)
> library(igraph)
> setwd("c:/Users/Adam/Desktop/RTwitter")
>
> rm(list=ls())
>
> data <-read.csv("TweetDataSTM.csv")
> processed <- textProcessor(data$documents, metadata = data)
Building corpus...
Error: is.Source(s) is not TRUE
In addition: Warning message:
In is.Source(s) : vectorized sources must have a positive length entry
> out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
Error in prepDocuments(processed$documents, processed$vocab, processed$meta) :
object 'processed' not found
> docs <- out$documents
Error: object 'out' not found
> vocab <- out$vocab
Error: object 'out' not found
> meta <-out$meta
Error: object 'out' not found
(any advice would be greatly appreciated!)
-Adam
I think your mistake occurs because your columns are named vocab and meta. But here
processed <- textProcessor(data$documents, metadata = data)
you are trying to call a column documents that - as far as I see - does not exist in your data.frame. Try changing the code to:
processed <- textProcessor(data$vocab, metadata = data)

Using geterrmessage() in a loop - R

My objective here is to capture the error that R throws and store it in an object.
Here are some dummy codes:
for(i in 1:length(a)){try(
if (i==4)(print(a[i]/"b"))else(print(a[i]/b[i]))
)}
[1] -0.125
[1] -0.2857143
[1] -0.5
Error in a[i]/"b" : non-numeric argument to binary operator
[1] -1.25
[1] -2
[1] -3.5
[1] -8
[1] Inf
[1] 10
So I want to capture that on the 4th iteration the error was: Error in a[i]/"b" : non-numeric argument to binary operator into an object say:
error<-()
iferror(error[i]<-geterrmessage())
I am aware that iferror as a function is not available in R, but I am trying to give the idea, because geterrmessage captures only the last error it sees
So for the example i want say for error[1:3]<-'NA'and error[5:10]<-'NA' because no error but
error[4]<-"Error in a[i]/"b" : non-numeric argument to binary operator"
So that later I can check error object and understand where and what error happened
If you can help me write a code that would be excellent and highly appreciated
I hope the following function helps:
a <- c(0:6)
b <- c(-3:3)
create_log <- function(logfile_name, save_path) {
warning("Error messages not visible. Use closeAllConnections() in the end of the script")
if (file.exists(paste0(save_path, logfile_name))) {
file.remove(paste0(save_path, logfile_name))
}
fid <- file(paste0(save_path, logfile_name), open = "wt")
sink(fid, type = "message", split = F) # warnings are NOT displayed. split=T not possible.
sink(fid, append = T, type = "output", split = T) # print, cat
return(NULL)
}
create_log("test.csv", "C:/Test/")
for(i in 1:length(a)){try(
if (i==4)(print(a[i]/"b"))else(print(a[i]/b[i]))
)}
closeAllConnections()

‘l’ is no list of MALDIquant::MassPeaks objects

I'm following this tutorial about MALDIquant package but i'm getting an error, before executing this line
peaks <- binPeaks(peaks, tolerance=0.002)
The Error is :
Error: binPeaks(peaks, tolerance = 0.002) : ‘l’ is no list of MALDIquant::MassPeaks objects!
When i do class(peaks) :
> class(peaks)
[1] "MassPeaks"
attr(,"package")
[1] "MALDIquant"
binPeaks just works on list of MassPeaks objects. Your class output shows that you have just a single MassPeaks object.
## load package
library("MALDIquant")
## create two MassPeaks objects
p <- list(createMassPeaks(mass=seq(100, 500, 100), intensity=1:5),
createMassPeaks(mass=c(seq(100.2, 300.2, 100), 395), intensity=1:4))
binnedPeaks <- binPeaks(p, tolerance=0.002)
## but not:
binPeaks(p[[1]])
# Error: binPeaks(peaks, tolerance = 0.002) : ‘l’ is no list of MALDIquant::MassPeaks objects!
Please look at ?binPeaks for details or write me an email (I am the maintainer, you will find my mail address at CRAN.

Unable to convert & assign character value to a numeric field

I have been struggling with the following issue:
I have the following variables:
class(HARdata)
[1] "data.frame"
dim(HARdata)
[1] 10299 88
class(activity_labels)
[1] "character"
length(activity_labels)
[1] 6
I have been trying to run the following loop:
for (i in 1:nrow(HARdata)) {
for (j in 1:length(activity_labels)){
if (as.numeric(HARdata[i, "traintype"]) == extract_numeric(activity_labels[j])) {
HARdata[i, "traintype"] <- activity_labels[j]
}
}
}
However, i get the following error:
Error in if (as.numeric(HARdata[i, "traintype"]) == extract_numeric(activity_labels[j])) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
NAs introduced by coercion
If I replace HARdata[i, "traintype"] <- activity_labels[j] with HARdata[i, "traintype"] <- 10 , the code runs fine. So I suppose the problem is in this line. The left side is numeric while the right side is supposed to be character. I tried running as.character(HARdata[i, "traintype"]) <- "test" but this doesn't work. Can anyone see what could be the issue?
test <- scan()
0.27513126 0.39694439
0.54228045 0.82751195
0.18600784 0.96602747
0.55259276 0.52368149
0.28976503 0.74500213
0.17534195 0.04931733
0.08077429 0.82169260
0.72602526 0.94921645
0.65077605 0.06989442
0.81399236 0.1379080
test <- as.data.frame(matrix(test, ncol=2))
names(test) <- c('cartype', 'traintype')
library(tidyr)
activity_labels <- c("$0.08077429", "$0.65077605")
test[,"traintype"][match(extract_numeric(activity_labels), test[,"traintype"])] <- activity_labels

Inscrutible error in plyr: Error in names(result) <- names(pieces) : 'names' attribute [11644] must be the same length as the vector [11187]

I'm getting an error while running plyr on a cluster, and I'm having a tough time debugging it. I'm sharing pseudo-code below. I run a function called getopt wrapped in Xgetopt, which handles errors. It basically does gridsearch optimization on a matrix of inputs, row-wise. The input matrix is 11644x2.
Can anybody tell me where this error comes from or what it means? I can't do traceback, because I can't replicate the error locally.
> library(plyr)
> library(doMC)
> registerDoMC(32)
>
> Xgetopt = function(input){
+ out = tryCatch(getopt(input), error=function(e) e, finally=NA)
+ if(inherits(out, "error")) {out=NA; print("an error happened but it got handled.")}
+ return(out)
+ }
>
> tocalc = expand.grid(ID = sort(unique(m$model$ID)), price = seq(from=0,to=100,by=2.5))
> tocalc$ID = as.character(tocalc$ID)
> out = dlply(.data=tocalc,c('ID','price'),.fun=Xgetopt,.parallel=TRUE)
[1] "an error happened but it got handled."
[1] "an error happened but it got handled."
[1] "an error happened but it got handled."
snip (only a few errors, mostly successes)
[1] "99 37.5"
Error in names(result) <- names(pieces) :
'names' attribute [11644] must be the same length as the vector [11187]
Calls: dlply -> llply
Execution halted

Resources