Can't replicate result with degree.distribution function of igraph - igraph

I found this very simple example online:
library(igraph)
g <- graph.ring(5)
plot(g)
summary(g)
degree.distribution(g)
I got the same results up to degree.distribution(g)while instead of [1] 0 0 1 I get NULL.
Since I have exactly the same problem with this example (NULL result to function degree.distribution instead of [1] 0.135 0.280 0.315 0.110 0.095 0.050 0.005 0.010) I wonder if the problem might be in the package installation.

The degree.distribution function in igraph 0.6.5 does not work well with R 3.0.0 and newer because of some changes in the return value of the hist function in R. The developers already know about it and it will be fixed in the next release. Until it is released, you have to work around the bug by modifying the source code of the degree.distribution function according to this patch.

Related

rdbwselect in R not showing output

I'm using the package rdrobust in R and Stata. I planned to fully implement the analysis in R, but encountered a problem with the function rdbwselect. This function computes different bandwidths depending on the selection procedure. By default, the procedure is Mean Square Error bwselect=mserd. However, I'm interested in exploring other procedures and comparing them. I then tried ALL=true; which is the option that according to the package "if specified, rdbwselect reports all available bandwidth selection procedures"
My issue is that, in R, rdbwselect is not showing me the bandwidths, not with the default not with the 'all' option or any other specification
x<-runif(1000,-1,1)
y<-5+3*x+2*(x>=0)+rnorm(1000)
## With default mserd
rdbwselect(y,x,)
## All selection procedures
rdbwselect(y,x,all= TRUE)
Output rdwselect
The output of both lines of rdbwselect code is exactly the same (see image), and it should not. I also try replicating the script from the rdrobust article in The R Journal (Page 49) and I don't get the same output as in the paper.
Nevertheless, the function is working in Stata 16
clear all
set obs 1000
set seed 1234
gen x = runiform(-1,1)
gen y = 5+3*x+2*(x>=0)+rnormal()
rdbwselect y x
rdbwselect y x, all
Could someone provide me with some guidance on why R is not showing me the complete expected output of the function rdbwselect? I'm wondering if this is an issue related to my version of R? Could this be a bug with the R package or the specific function rdbwselect? How can I verify the computation behind rdbwselect?
I appreciate any advice or follow-up questions.
Found the solution. All I needed to do was to embed the function within the summary() function
summary(rdbwselect(y,x,))
or add a pipe and the summary function
rdbwselect(y,x,all= TRUE) %>%
summary()
I want to post it as this is nowhere mentioned in the package documentation nor the article in The R Journal.

Status of in-place `rfft` and `irfft` in Julia

So I'm doing some hobby-related stuff which involves taking Fourier transforms of large real arrays which barely fit in memory, and was curious to see if there was an in-place version of rfft and irfft that saved RAM, since RAM consumption is important to me. These transforms are possible despite the input-vs-output-type mismatch, and require an extra row of padding.
In Implement in-place rfft! and irfft!, Tim Holy said he was working on an in-place rfft! and irfft! that made use of a buffer-containing RCpair object, but then Steven Johnson said that he was implementing something equivalent using A_mul_B!(y, plan, x), which he elaborated on here.
Things get a little weird from then on. In the documentation for both 0.3.0 and 0.4.0 there is no mention of A_mul_B!, although A_mul_B is listed. But when I try entering them into Julia, I get
A_mul_B!
A_mul_B! (generic function with 28 methods)
A_mul_B
ERROR: A_mul_B not defined
which suggests that the situation is actually the opposite of what the documentation currently describes.
So since A_mul_B! seems to exist, but isn't documented anywhere, I tried to guess how to test it in-place as follows:
A = rand(Float32, 10, 10);
p = plan_rfft(A);
A_mul_B!(A,p,A)
which resulted in
ERROR: `A_mul_B!` has no method matching A_mul_B!(::Array{Float32,2}, ::Function, ::Array{Float32,2})
So...
Are in-place real FFTs still a work in progress? Or am I using A_mul_B! wrong?
Is there a mismatch between the 0.3.0 documentation and 0.3.0's function library?
That pull request from Steven Johnson is listed as open, not merged; that means the work hasn't been finished yet. The one from me is closed, but if you want the code you can grab it by clicking on the commits.
The docs indeed omit mention of A_mul_B!. A_mul_B is equivalent to A*B, and so isn't exported independently now. A_mul_B! would be used like this: instead of C = A*B, you could say A_mul_B!(C, A, B).
Can you please edit the docs to fix these issues? (You can edit files here in your webbrowser.)

foreach within package function: does not work on first call

I am trying to add parallel computation option to an R (netresponse) package based on doMC and multicore. The script works ok, but only on the second trial.
To reproduce the bug, start R and run the script below. It gets stuck on the last line. After interrupting with ctrl-c I get a few messages of "select: Interrupted system call". Then, running the same script again will give the expected result without problems.
Is some further initialization needed to get this work properly already on the first run? Or any other tips?
thanks for your support,
- L
require(netresponse)
require(multicore)
require(doMC)
registerDoMC(3)
print(getDoParWorkers())
res <- foreach(i = 1:100, .combine = cbind,
.packages = "netresponse") %dopar% netresponse::vdp.mixt(matrix(rnorm(1000), 100, 10))
Heres the list of dependencies from the help page for package netresponse: "Depends: methods, igraph, graph, minet". I suspect that you are not getting all of them to the workers by just listing "netresponse" on the .packages argument.
Quick fix for problem with foreach %dopar% is to reinstall these packages:
install.packages("doSNOW")
install.packages("doParallel")
install.packages("doMPI")
As mentioned in various threads at StackOverflow, these are responsible for parallelism in R. Bug which existed in old versions of these packages is now removed. I should mention that it will most likely help even though you are not using these packages in your project/package.

R: 2 functions with the same name in 2 different packages

I need to load to R packages : tseries and chron
Both have a function named is.weekend
I always have in my environment the function from the second package I loaded.
How can I access always the function from, let say, chron ?
You have probably already noticed that the order of loading the packages makes a difference, i.e. the package that gets loaded last will mask the functions in packages loaded earlier.
To specify the package that you want to use, the syntax is:
chron::is.weekend()
tseries::is.weekend()
In other words, use packagename::functionname()
In addition, if you know that you will always want to use the function in chron, you can define your own function as follows:
is.weekend <- chron::is.weekend #EDIT
library(chron)
is.weekend.chron <- is.weekend
library(tseries)
then you can call is.weekend for the tseries version or is.weekend.chron for the chron version
you should turn to the conflicted package from Hadly.
library(conflicted)
library(dplyr)
filter(mtcars, am & cyl == 8)
Then the conflicted package will throw an error and will force you clearly determine which function you prefer:
Error: filter found in 2 packages. You must indicate which one you want with ::
* dplyr::filter
* stats::filter
To resolve conflicts for your entire session, use <-:
filter <- dplyr::filter
filter(mtcars, am & cyl == 8)
mpg cyl disp hp drat wt qsec vs am gear carb
1 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4
2 15.0 8 301 335 3.54 3.57 14.6 0 1 5 8
You can also turn to the conflict_prefer() function which can determine the winner when a conflict occur.
The code example is borrowed from Hadly, pls refer to the package website.
https://www.tidyverse.org/blog/2018/06/conflicted/
i had 2 packages who had same function name ts()
The 2 pacckages who had the same were :
forecast
List item
I inspected what was going on by typing
?ts
Help on topic 'ts' was found in the following packages:
Time-Series Objects
(in package stats in library C:/Program Files/R/R-3.6.2/library)
Format time stamps
(in package bReeze in library C:/Users/mycomputer/Documents/R/win-library/3.6)
Solution : Then to use the function ts that comes with package forecast
i used : because the help showed me that forcast was calling stats
Time-Series Objects (in package stats
stats::ts
because is saw from help that forecasts use a package called stats ;)
forecast::ts
Time-Series Objects
(in package stats
was giving me error, because forecast package was using a sub package ;
so final usage looks like this :
library(bReeze)
library(forecast)
# Subset data
my_time_series <- stats::ts(c(df_sub$y), start=2018, frequency = 12)
# Plot
theme_set(theme_classic())
ggseasonplot(my_time_series) + labs(title="My graph title")

MATLAB integration of an anonymous function

I wanted to ask how can I can write this in MATLAB.
I want to integrate fp(z) with z(0,x). I tried this :
fpz=#(z) f1x(z) ./ quadl(f1x(z),0,1);
sol=int(fpz,0,x) --> i also tried sol=quadl(fpz,0,x)
y=solve('y=sol',x)
xf=# (y) y ; -->this is the function i want
where f1x=# (x) 1 ./(x.^2+1) and fpx = #(x) f1x(x) ./ quadl(f1x,0,1);
but it doesn't work.
Hello,thanks for helping.
The problem is that i want an analytically solution and i can't get one.
I want f1x to give me " 1/x^2+1" , fpx "4/pi*(1+x^2) and fpz "4ArcTan(x)/pi", instead of giving me "f1x=# 1./(x^2+1)"..
With the code you send me ,still the same problem.
I managed to come into this :
f1x=# (x) 1 ./(x.^2+1)
fpx = #(x) f1x(x) ./ quadl(f1x,0,1)
f2z=# (z) 1 ./(z.^2+1);
fpz=#(z) fpx(z) ./ quadl(f2z,0,1)
sol=int(fpz(z),z,0,x)
y=solve(subs('y=sol'),x)
xf=# (y) y
The "sol" and "y=" gives me analytically answer but it is wrong because i assume f1x and fpx,fpz doesn't return into analytically expressions.
0 Your definition of fpz doesn't make any sense; as Marcin already said, you're trying to integrate something that isn't a function. This shouldn't be a problem for your alternative version with f2z. I think the code in the original question should have had just f1x rather than f1x(z) in the first line.
1 Your revised version with f2z has a different problem: now in fpz you aren't actually providing the function with an argument.
The following code seems to be the kind of thing you had in mind, and works fine for me (in MATLAB R2008a, as it happens, but none of this should be different in other versions):
f1x = #(x) 1 ./ (x.^2+1);
fpx = #(x) f1x(x) ./ quadl(f1x,0,1);
fpz = #(z) fpx(z) ./ quadl(fpx,0,1);
Now evaluating fpz(3), for instance, spins for about half a second (on my old slow laptop computer) and returns 0.1273.
So I think the problems you've been having with integration of anonymous functions are just a matter of not being quite careful enough to distinguish between the function itself and a particular value of the function.
You have some further questions about the "solve" and "int" functions in the Symbolic Math Toolbox. You should take the following with a pinch of salt because I don't have that toolbox and am relying on the online documentation for it.
3 You're feeding the names of functions you've defined in MATLAB to the Symbolic Math Toolbox functions. I don't think that is supposed to work; "int" and "solve" expect explicit algebraic expressions, not MATLAB functions. (And, further, your functions all use numerical integration -- quad, quadl, etc. -- and there's no possible way that the symbolic functions can do anything useful with that.)
Finally: When you're asking questions of this sort, it's helpful if rather than "it doesn't work" you say how it doesn't work. For instance, your most recent comment is much more useful ("it gives me ..." followed by the actual output you get from MATLAB).