How to improve the precison in such a situation in R? - r

I want to calculate the value of (1e-6)^84, but in R/R64, the result is 0, which would cause some problem when applying log10 function on it.
Is there anyway to solve this problem?

Depends what problem you are actually trying to solve. Do you care about the value of log(teeenytinynumber)? If not, replace the zero values with NA and keep going. If you do, figure out if there's a better way than following a giant exponent with a log function. Which is to say, simplify your algorithm before crunching numbers.

Related

The Period of Data Using TI-BASIC

I'm in basic trigonometry and currently learning how to find equations having been given the data only. I understand the concept pretty well, but I usually make a program in a TI-BASIC for solving my homework because it helps me understand it at a deeper level and gain an appreciation for the beauty of math, however this time I'm stumped. Is there a known way to take pure data and find it's period or frequency in a way that can be fully automated on TI-BASIC?
I think I have some potential solutions:
If I can figure out getting a mode on a TI-84 I can just figure out the space between the two numbers who are a part of the mode.
Safely decrease the range of the numbers to make the data more manageable, such as making the numbers between -1 and 1 and finding the space between 1 and the next 1
Guess probable equations and just figure it out through brute force
An example would be finding the period on This Table, hopefully, this makes sense, and if there isn't a known way that's okay. Thank you for your time!

Effect of setting seeds on an algorithm

I am writing an R code where, I am using set.seed() function in the whole program to generate the data and then using it in a function , ultimately plotting the function and then using optim to get the minima. But now the issue is the graphs of the function changes if I change the seed value and sometimes doesn't even produce a concave graph but an exponential graph.
I am not able to understand why this is happening and how I can fix it. If anyone can provide me with any reference to read in this subject or any suggestions as to what can be done, that will be great.
Thanks in advance
set.seed() configures the random number generator to start from that seed. This may be a bit more complicated, depending on the precise implementation, but the effects are always the same: The sequence of numbers will be identical.
This is useful in a number of applications where you want some randomness, but you want to get the same result if you re-run the code. Say for example you need to randomly sample your data, but since you are debugging, it's useful if you get the same sample so that the bugs don't disappear on you.
Also if you want other people to replicate the results, you simply pick some random number as the seed and tell them that you used that seed. Anything in the algorithm based on random numbers will behave the same because you are both using the same sequence of numbers.
For your graph problem you need to share some code so that people understand what you are doing. It's very hard to guess what went wrong. At the outset it seems that you algorithm is very strongly influenced by the random numbers (usually not a good sign).
In simple, if you set a seed, and extract a random number, the random number will be always the same. If you not set a seed, every time you choose a number the number will be different. The seed permit you to replicate your experiment.

Solve Constrained Quadratic Programming with R

I really love R but from time to time it really gives me a headache...
I have the following simple quadratic minimization problem which can be formulated and solved within no time in Excel (click on picture to enlarge):
and
The problem itself is pretty straightforward: I want to minimize (w1^2+w2^2)/2 by finding the best combination of w1,w2 and b under the constraints that for all Y*(w1*X1+w2*X2+b) >= 1
I know that there is the quadprog package for solving these kinds of problems but I find it so unintuitive that I am not able to specify the problem correctly :-( I hate to say it but Excel seems to be better suited for specifying optimization problems like these :-(((
My question
How to formulate the above problem correctly so that it can be solved with R (no matter which package) and the program arrives at the correct values for w1, w2 and b (as can be seen in the picture above). Please don't just post links but please give actual code that works. It would be great if you could comment your code so it becomes clear why you do the things you do. Thank you!
The necessary data is here:
data <- matrix(c(2.947814,6.626878, 1,
2.530388,7.785050, 1,
3.566991,5.651046, 1,
3.156983,5.467077, 1,
2.582346,4.457777,-1,
2.155826,6.222343,-1,
3.273418,3.520687,-1),ncol=3,byrow=T)
colnames(data) <- c("X1","X2","y")
Addendum
Some people took offense at my request to provide code (and not simply links). I apologized for that and gave my reasons that I did not find any good approaches in the answers so far on SO. The deeper reason for that is that the problem is unusual in the sense that b is only in the constraint and not in the objective function. So I still think that this question is a good fit for SO.
Actually, the problem is a little tricky because b is only present in the inequality constraint matrix but not in the objective function. Therefore the matrix in the quadratic programming problem is only positive semidefinite but not positive definite.
My approach is therefore to set the matrix entry corresponding to b to a very small value - in my case 1e-9. Someone else more familiar with such optimization problems might know how to solve the problem properly...
Calculate solve.QP input
c1=data[,"X1"]*data[,"y"]
c2=data[,"X2"]*data[,"y"]
#I use 1e-9 for the b entry
Dmat=matrix(`[<-`(numeric(9),c(1,5,9),c(1,1,1e-9)),3,3)
dvec=rep(0,3)
Amat=cbind(c1,c2,data[,"y"])
bvec=rep(1,nrow(Amat))
Solve with solve.QP
library(quadprog)
sol=solve.QP(Dmat=Dmat,dvec=dvec,Amat=t(Amat),bvec=bvec)$solution
sol
#[1] 2.903910 1.201258 -14.734964
Same as excel.

R returns NULL when trying to extract P value

I am running a PGLS loop on 1,000 trees and I am trying to extract slope, standard error, and P-values from my loop. Slope and standard error are easily extracted by using data$model$coef[2] and data$sterr[2] but the p value just does not want to come out. I have tried
1)summary(result)$pvalue which gives value NULL
2)data$model$coef[2,4] which says this is out of bounds (in fact playing around I couldn't access anything outside of the first column of coefficients without receiving the same error message)
I have tried a slew of other methods that are more or less the same idea as these two and each time I either get the dreaded NULL, the occasional NA or the out of bounds error. Does anyone know what is going on? I know we typically provide the data but this seems like a menial question (although I have spent significantly more time trying to resolve this that in my actual PGLS).
I do not think it is possible directly See this (old) question. Perhaps the easiest way is to use the summary object to extract the necessary information to calculate it yourself (via a helper function)?
See this also,

A combination of commands partial_fraction(x) and coefficient(x,n)

I am trying to do some iterative calculations where each time SAGE constructs a fraction and lists the coefficients in the partial fraction decomposition of that fraction. I realized that doing everything symbolically, SAGE wants to keep everything as integer as possible. So what it does is to simplify fractions like 1/(x-3/2) as 2/(2x-3) and then when I ask for
f.coefficient(x-3/2,-1)
it returns 0, while I expect it to return 1.
I have tried to solve things numerically, but there are two problems:
The errors get really big after each iteration
It takes much much longer to calculate it
Any suggestions to get SAGE to solve this is greatly appreciated.

Resources