Invalid number of breaks? - r

I'm trying to make a confidence interval for practice and I keep getting an error referring to:
an 'invalid number of breaks' at hist.default(boot.dist).
I'm pretty sure the problem is somewhere here.
Any advice or help would be very much appreciated at this point.
b=1000
boot.dist = rep(0,b)
for (i in 1:b) {
boot.sample = sample(ACS$Income, replace = TRUE)
boot.dist[i] = mean(boot.sample)
}
hist(boot.dist)

The problem is that ACS$Income is array of NA.
Example, this code will reproduce error exactly like yours:
boot.dist[1:1000]<-NA
hist(boot.dist )
Error in hist.default(boot.dist) : invalid number of 'breaks'

Related

TypeError: argmax(): argument 'input' (position 1) must be Tensor, not str

My code was working fine and when I tried to run it today without changing anything I got the following error:
TypeError: argmax(): argument 'input' (position 1) must be Tensor, not str
Would appreciate if help could be provided.
below is the code snippet where I am getting an error. I am using BERT mdoel
start_scores, end_scores = model(torch.tensor([input_ids]), # The tokens representing our input text.
token_type_ids=torch.tensor(
[segment_ids])) # The segment IDs to differentiate question from answer_text
# ======== Reconstruct Answer ========
# Find the tokens with the highest `start` and `end` scores.
answer_start = torch.argmax(start_scores)
answer_end = torch.argmax(end_scores)
In your line of code
start_scores, end_scores = model(torch.tensor([input_ids]),token_type_ids=torch.tensor([segment_ids]))
you have to make it:
start_scores, end_scores = model(torch.tensor([input_ids]),token_type_ids=torch.tensor([segment_ids]), return_dict=False).
It worked for me. I had the same problem.

Error when trying to add a row to a dataframe

I am pretty noob in R and this is driving me crazy. When I try to add a new row to this data frame it gives me this strange error and instead of adding a new name it adds . Does anyone know how to solve this? I really need help
MENU <- function()
{
P1=readline(prompt="Insert Name1:")
P2=readline(prompt="Inset Name2:")
TA=data.frame(names=c(P1,P2),games=c(0,0),points=c(100,100),stringsAsFactors= F)
write.table(TA,"C://Users//aleja//Documents//names.txt")
TA=read.table("C://Users//aleja//Documents//names.txt")
P1=readline(prompt="Insert Name1:")
new=list(names=P1,games=0,points=100)
TA=rbind(TA,new)
}
So this is what happens:
Insert Name1:ale
Inset Name2:ana
Insert Name1:jose
**Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "jose") :
invalid factor level, NA generated**
Thanks for the help :)
To keep everything in your function the same, it would be fixed by:
MENU<-fucntion()
{
P1=readline(prompt="Insert Name1:")
P2=readline(prompt="Inset Name2:")
TA=data.frame(names=c(P1,P2),games=c(0,0),points=c(100,100),stringsAsFactors= F)
write.table(TA,"C://Users//aleja//Documents//names.txt")
TA=read.table("C://Users//aleja//Documents//names.txt")
P1=readline(prompt="Insert Name1:")
new=data.frame(names=P1,games=0,points=100)
TA=rbind(TA,new)
}
Hope this helps,

R error: dims do not match the length of an object

I am currently trying to run some code (if you need to know the purpose to help me, ask me, but I'm trying to keep this question short). This is the code:
par<-c(a=.5,b=rep(1.3,4))
est<-rep(TRUE,length(par))
ncat<-5
Theta<-matrix(c(-6,-5.8,-5.6,-5.4,-5.2,-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5,5.2,5.4,5.6,5.8,6))
p.grm<-function(par,Theta,ncat){
a<-par[1]
b<-par[2:length(par)]
z<-matrix(0,nrow(Theta),ncat)
y<-matrix(0,nrow(Theta),ncat)
y[,1]<-1
for(i in 1:ncat-1){
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
}
for(i in 1:ncat-1){
z[,i]<-y[,i]-y[,i+1]
}
z[,ncat]<-y[,ncat]
z
}
However, when I try to run the code:
p.grm(par=par,Theta=Theta,ncat=ncat)
I get the following error:
Error: dims [product 61] do not match the length of object [0]
Traceback tells me that the error is occurring in the first for loop in the line:
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
Could someone point me to what I'm doing wrong? When I try to run this code step by step outside of the custom p.grm function, everything seems to work fine.
It is a common mistake. When you write the for loop and you want it from 1 to ncat -1 remember to write it as for (i in 1:(ncat-1)) instead of for(i in 1:ncat-1) they are completly different.
You may also add to the function something to return return(z). Here it is the corrected code:
par<-c(a=.5,b=rep(1.3,4))
est<-rep(TRUE,length(par))
ncat<-5
Theta<-matrix(c(-6,-5.8,-5.6,-5.4,-5.2,-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5,5.2,5.4,5.6,5.8,6))
p.grm<-function(par,Theta,ncat){
a<-par[1]
b<-par[2:length(par)]
z<-matrix(0,nrow(Theta),ncat)
y<-matrix(0,nrow(Theta),ncat)
y[,1]<-1
for(i in 1:(ncat-1)){
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
}
for(i in 1:(ncat-1)){
z[,i]<-y[,i]-y[,i+1]
}
z[,ncat]<-y[,ncat]
return(z)
}
p.grm(par=par,Theta=Theta,ncat=ncat)

HMM Error in if (d < delta) { : missing value where TRUE/FALSE needed

I have some problems with the viterbiTraining function from the HMM package.
I tried using it on a pretty straightforward hmm and a vector of observations.
Here's the code:
Emisije<-rep("IntervalC",length(Cl1.res))
Emisije[IntervalA[,1]]<-"IntervalA"
Emisije[IntervalB[,1]]<-"IntervalB"
The Emisije vector looks like this:
head(Emisije)
[1] "IntervalA" "IntervalA" "IntervalA" "IntervalC" "IntervalB" "IntervalA"
startProbs<-c(0.6873065,0.3126935)
transProbs<-matrix(c(0.8, 0.7, 0.2,0.3),ncol=2)
emissionProbs<-matrix(rep(1/3,6),ncol=3)
stanji<-initHMM(c("NizkaVar", "VisokaVar"), c("IntervalA", "IntervalB",
"IntervalC"), startProbs, transProbs, emissionProbs)
After running this everything works, except for the viterbiTraining function, which gives the following result:
viterbiTraining(stanji,Emisije)
Error in if (d < delta) { : missing value where TRUE/FALSE needed
Even the similar function baumWelch, which takes the exact same parameters, works without errors, so I really don't understand what's wrong here.
Can anyone please explain to me what I am doing wrong? Thank you in advance.

Unable to resolve an Argument is of length zero error error

I get an Argument is of length zero error when I run the below code
The code is from this blog -http://giventhedata.blogspot.in/2012/08/r-and-web-for-beginners-part-iii.html.
library(XML)
url<- "http://news.bbc.co.uk/2/hi/uk_politics/8044207.stm"
first<-"Abbott, Ms Diane"
url.tab <- readHTMLTable(url)
for (i in 1:length(url.tab)){
if (as.character(url.tab[[i]][1,1]) == first ) {print(first)}
}
I know that the url.tab[[5]][1,1]) does contain the string "Abbott, Ms Diane", and when I run IF statement in isolation replacing the i with 5, it runs fine. Any help would be appreciated. I also tried declaring i<-1 upfront. DInt change anything.
Some of your tables are in fact NULL.
So you have to test for is.null before trying to subset the table:
for (i in 1:length(url.tab)){
this.tab <- url.tab[[i]]
if(!is.null(this.tab)) if(as.character(this.tab[1,1]) == first ) {print(first)}
}
[1] "Abbott, Ms Diane"

Resources