How to automatically skip over errors in r - r

I'm trying to create new variables from some output from a number of two-piece segmented regression models that I'm running. The code for my new variable is:
initial1=c(fmod$psi[1],fmod2$psi[1], fmod3$psi[1], fmod4$psi[1], fmod5$psi[1], fmod6$psi[1], fmod7$psi[1], fmod8$psi[1], fmod9$psi[1], fmod10$psi[1], fmod11$psi[1],fmod12$psi[1], fmod13$psi[1], fmod14$psi[1], fmod15$psi[1], fmod16$psi[1], fmod17$psi[1], fmod18$psi[1], fmod19$psi[1], fmod20$psi[1], fmod21$psi[1],fmod22$psi[1], fmod23$psi[1], fmod24$psi[1], fmod25$psi[1], fmod26$psi[1], fmod27$psi[1], fmod28$psi[1], fmod29$psi[1], fmod30$psi[1], fmod31$psi[1],fmod32$psi[1], fmod33$psi[1], fmod34$psi[1], fmod35$psi[1], fmod36$psi[1], fmod37$psi[1], fmod38$psi[1], fmod39$psi[1], fmod40$psi[1], fmod41$psi[1],fmod42$psi[1], fmod43$psi[1], fmod44$psi[1], fmod45$psi[1], fmod46$psi[1], fmod47$psi[1], fmod48$psi[1], fmod49$psi[1], fmod50$psi[1], fmod51$psi[1],fmod52$psi[1], fmod53$psi[1], fmod54$psi[1], fmod55$psi[1], fmod56$psi[1], fmod57$psi[1], fmod58$psi[1], fmod59$psi[1], fmod60$psi[1], fmod61$psi[1], fmod62$psi[1], fmod63$psi[1], fmod64$psi[1])
where fmod, fmod2, fmod3, etc. are my regression models. Some of the regression models have errors and do not produce output (because the initial breakpoint estimates are too close to each other). Because of that, when I try to make my 'initial1' variable, I get error messages such as:
Error: object 'fmod12' not found
and the 'initial' variable is not created. I would like these models that don't have output associated with them to be automatically skipped over, or to be replaced with 'NA'. Does anyone know how to do this?

You're creating many different models and giving them numbered names. Why not put them in a list instead?
At model creation time:
for (i in 1:lots) fmod[[i]] <- my_segmented_reg(...)
where my_segmented_reg presumably returns either a model, or NULL or NA.
Then you have a list fmod which you can start using straight away.

Related

Loop in R not functioning correctly

I have problems with my coded loop for some of my variables. I have run it firstly with one variable and it ran fine. Upon entering a different variable and different data range, the loop did not include all the outputs that it should have done according to the loop. There was no error message. The code for the loop is below:
geartype=data[179:187]
data_stats_structure_gt=as.data.frame(matrix(nrow=0,ncol=3))
names(data_stats_structure_gt)=c('Gear.Type','Man_Kendall_pvalue','Man_Kendall_tauv')
for (i in 1:ncol(geartype)){
dat=geartype[,i]
dat=as.data.frame(cbind(data$ï..Year,dat))
ds=names(geartype)[i]
names(dat)=c("Year",names(geartype)[i])
dat=dat[-which(is.na(dat[,2])==TRUE),]
if(nrow(dat)>=3){
output=as.data.frame(matrix(nrow=1,ncol=3))
names(output)=c('Gear.Type','Man_Kendall_pvalue','Man_Kendall_tauv')
output$Gear.Type=names(geartype)[i]
output$Man_Kendall_pvalue=mk.test(dat[,2])$p.value
output$Man_Kendall_tauv=mk.test(dat[,2])$estimates[3]
data_stats_structure_gt=rbind(data_stats_structure_gt,output)
}
}

Xcos throws "Undefined variable: scifunc_block_m" message in console

When I run a Xcos model containing a scifunc_block_m block like shown below
I get an error message relating to data dimensions inconsistency:
"Data dimensions are inconsistent:"
" Variable size=[1,1]"
"Block output size=[100,1]."
But when I double click in the block in order to see what can I change to make the dimensions correct I get a message in the console saying
Undefined variable: scifunc_block_m
What bugs me is that scifunc_block_m is not the name of any variable, but rather the name of the block itself like can be seen in the official docs.
Of course I double checked that nowhere in my function phase_shifter neither anywhere else I have any variable named like that.
I tried with Scilab 6.1.1 and 6.1.0 believing that it might be a bug from apparently not.
In your phase_shifter.sce file generating the input variable,
the signalIn variable does not comply with the From Workspace block requirements, whose documentation says that the input variable
must be a structure with time and values fields
.time must be a column vector, and in your case
.values must also be a column
So,
t = (0:1/fs:Npp/fs - 1/fs); // time vector
signalIn = A*%e^(%i*w*t);
should be replaced with
t = (0:1/fs:Npp/fs - 1/fs)'; // time column vector
signalIn = struct("time",t, "values",A*%e^(%i*w*t));
This fixes the inconsistent dimensions message.
In addition, i am not able to reproduce your issue about Undefined variable: scifunc_block_m. The parameters interface opens as expected.
You may get this kind of messages if you try to run some xcos parts out of xcos, without beforehand loading xcos-related libraries.
Then, we get an unclear "Output should be of complex type." message on the From workspace block.
By the way, you try to plot some complex values. Please have a look to the MATMAGPHI block before entering MUX: https://help.scilab.org/docs/6.1.1/en_US/MATMAGPHI.html

How do I create a loop function to apply acoustic indices from "soundecology" to specific sections of .wav files using R

I have a large quantity of .wav files that I need to analyze using the acoustic indices from the "soundecology" package in R. However, the recordings do not have uniform start times and I need to analyze specific periods of time within the files. I want to create a function and loop for automating the process.
I have created a spread sheet for each folder of recordings (each folder is a different location) that lays out the recording and the times within each recording that I need to analyze. Basically, a row contains: the sound file name, the time when the sample should start (eg. 09:00:00, the number of seconds from the start of the file that that time occurs, and the munber of seconds from the start time of the file that the end of the sample should occur.
That data looks like this:
Spread sheet of data
I am using the package "tuneR" and "warbleR" to select the specific portion of a sound file that I want to analyze. Here is the the code and the output that I would like to loop across all the sound files:
wavrow1 <-read_wave(mvb$sound.files[1], from = mvb$start[1], to = mvb$end[1])
wavrow1.aci <- acoustic_complexity(wavrow1, j=10)
which yeilds
max_freq not set, using value of: 22050
min_freq not set, using value of: 0
This is a mono file.
Calculating index. Please wait...
Acoustic Complexity Index (total): 934.568
However, when I put this into a function in order to then put it into a loop I get a different output.
acianalyzeFUN <- function(mvb, i){
r <- read_wave(mvb$sound.files[i], mvb$start[i], mvb$end[i])
soundfile.aci <- acoustic_complexity(r, j=10)
}
row1.test <- acianalyzeFUN(mvb, 1)
This gives the output:
max_freq not set, using value of: 22050
min_freq not set, using value of: 0
This is a mono file.
Calculating index. Please wait...
Acoustic Complexity Index (total): 19183.03
Acoustic Complexity Index (by minute): 931.98
Which is different.
So I need to fix this function and put it into a loop so that I can apply it across all the files and save the results into a data frame or ultimately another spread sheet.
I was thinking a loop like the following might work but I am also getting errors with it:
output <- vector("logical", length(97))
for (i in seq_along(mvb$sound.files)) {
output[[i]] <- acianalyzeFUN(mvb, i)
}
Which returns this error:
max_freq not set, using value of: 22050
min_freq not set, using value of: 0
This is a mono file.
Calculating index. Please wait...
Acoustic Complexity Index (total): 19183.03
Acoustic Complexity Index (by minute): 931.98
Error in output[[i]] <- acianalyzeFUN(mvb, i) :
more elements supplied than there are to replace
Thanks for any help and advice on this. Please let me know if there are any other pieces of information that would be helpful.
the read_wave function takes following arguments :
read_wave(X, index, from = X$start[index], to = X$end[index], channel = NULL,
header = FALSE, path = NULL)
In the manual test, you specify from = mvb$start[1], to = mvb$end[1]
In the function you created, you dont specify the arguments :
r <- read_wave(mvb$sound.files[i], mvb$start[i], mvb$end[i])
so that mvb$start[i] gets affected to index and mvb$end[i] to from.
You should write:
acianalyzeFUN <- function(mvb, i){
r <- read_wave(mvb$sound.files[i], from = mvb$start[i], to = mvb$end[i])
soundfile.aci <- acoustic_complexity(r, j=10)
}
This should explain the difference you observe.
Regarding the error, you create a vector of logical to collect the result, but acianalyzeFUN returns nothing : it just sets two variables r and soundfileaci without returning anything.

How can I change a CPLEX parameter in my Julia code?

I'm using the CPLEX solver to run my ILP model.The ILP model is implemented with Julia/MultiJuMP.
I would like to limit the time of optimization of the problem. If I were working with OPL, I would just have to add Cplex.tilimt=100
In Julia, I put the following code :
mmodel = MultiModel(solver = CplexSolver("CPLEX.tilim"=100), linear = true)
It doesn't work.
From the last section in https://github.com/JuliaOpt/CPLEX.jl/blob/master/README.md, it appears that Julia uses the legacy parameter names as they appear in the C API of CPLEX. For example, CplexSolver(CPX_PARAM_EPINT=1e-8).
Here's the link to the the CPLEX documentation for that parameter: https://www.ibm.com/support/knowledgecenter/SSSA5P_12.9.0/ilog.odms.cplex.help/CPLEX/Parameters/topics/EpInt.html. As you can see, the name appears as the first row in the 'Name prior to V12.6.0' column.
For the time limit, you should thus use CPX_PARAM_TILIM, as this is the name in https://www.ibm.com/support/knowledgecenter/SSSA5P_12.9.0/ilog.odms.cplex.help/CPLEX/Parameters/topics/TiLim.html.

glm probit "cannot find valid starting values" error message

I am trying to use R for the first time to do some probit analaysis.
I get the following error:
Error in if (!(validmu(mu) && valideta(eta))) stop("cannot find valid starting values: please specify some", :
missing value where TRUE/FALSE needed
This is the command I am using:
m1=glm(Good~Stg.Days+Dev.Deployments+Check.Ins+NoOfDevelopers,family=poisson(link = "probit"),data=deploy[1:4,])
My data deploy[1:4,] are as loaded in from a CSV file follows:
Good,Application Type,Project,Start Date,End Date,Stg Days,Dev Deployments,Check Ins,NoOfDevelopers
1,DocumentPlatform,ZCP,11/08/2010,11/11/2010,0.6,0,12,4
1,DocumentPlatform,ZCP,11/11/2010,09/12/2010,0.4,0,4,1
0,DocumentPlatform,ZCP,09/12/2010,07/03/2011,10,0,7,3
1,FactsheetPlatform,Process.ARCH,28/06/2010,09/03/2011,7.1,0,18,2
deploy is in reality a much bigger vector than 1:4 I am just using a subset of the data to help determine the problem.
Any ideas what is wrong?
As i commented: Using ?glm I found tha the poisoon family supports the following links: log, identity, and sqrt.
Testing on another link:
test <- data.frame('Good'=c(1,1,0,1),'Stg Days'=c(0.6,0.4,10,7.1),'Dev Deployments'=c(0,0,0,0),'Check Ins'=c(12,4,7,18),'NoOfDevelopers'=c(4,1,3,2))
m1=glm(Good~ . ,family=poisson(link = "log"),data=test)
Gives no errors. So I think your link = "probit" is the problem.

Resources