This is probably very simple, but I am having trouble getting R to loop through negative numbers. I have tried using seq(-10,10,1), I have tried using seq(from=-10, to=10, by=1) What I get is either b values of 1-10 or 1-21. I need it to be from -10 to 10. What am I missing in here? Any help is very appreciated.
Here is my current attempt at the code:
pr1 <- NULL
i <- 1
t = seq(from = -10, to = 10, by = 1)
for (b in seq(t)) {
LLF <- sum(1*log((1/(1 exp(-(b))))) (1-1)*log(1-(1/(1 exp(-(b))))))
like[b] <- LLF
pr[b] <- b
}
pr
df <-data.frame(like, pr)```
CodePudding user response:
You just need for (b in t) {
because t already represents the sequence you want.
CodePudding user response:
Several errors. Failing to distinguish values from their integer order and failing to initialize variables used in a for-loop:
pr1 <- NULL
i <- 1
(t = seq(from = -10, to = 10, by = 1) )
like <- numeric(); pr <- numeric() # need to exist to do subscripted assignment
for (b in seq_along(t) ) {
# Note the use of b used to index the `t` vector
LLF <- sum(1*log((1/(1 exp(-( t[b] ))))) (1-1)*log(1-(1/(1 exp(-( t[b] ))))))
like[b] <- LLF
pr[b] <- b
}
pr
df <-data.frame(like, pr)
Probably NOT a good idea to use t
as a variable name. It's an R function name that transposed matrices. Same goes for df
which is the density function for the F distribution. Thos are not errors but cautions to prevent strange error messages and confusion amont humans. R can keep object names distinct from function names.