Weird behavior of is.numeric()

An interesting observation. Please note that I have absolutely no idea about why this happens. (at least not at the time of first writing this)

In our datalab, ingeniuously named “Datalab” (the one at KUB-North, because contrary to the labs at the libraries for social sciences, and for the humanities, we are not allowed to have a name), we were visited by at student.

She wanted to make a dose-response plot. Something about the concentration of something, giving some clotting of some blood. Or something…

Anyway, she wanted to do a 4-parameter logistic model. Thats nice, I had never heard about that before, but that is the adventure of running a datalab, and what makes it fun.

Of course there is a package for it, dr4pl. After an introduction to the wonderful world of dplyr, we set out to actually fit the model. This is a minimal working example of what happened:

library(tidyverse)
library(dr4pl)

data <- tibble(dose= 1:10, response = 2:11)

dr4pl(data, dose, response)
## Error in dr4pl.default(dose = dose, response = response, init.parm = init.parm, : Both doses and responses should be numeric.

WTF? “Both doses and responses should be numeric”?. But they are!

is.numeric(data$dose)
## [1] TRUE
is.numeric(data$response)
## [1] TRUE

The error is thrown by these lines in the source:

if(!is.numeric(dose)||!is.numeric(response)) {
    stop("Both doses and responses should be numeric.")
  }

Lets try:

if(!is.numeric(data$dose)||!is.numeric(data$response)) {
    stop("Both doses and responses should be numeric.")
  } else {
    print("Where did the problem go?")
  }
## [1] "Where did the problem go?"

No idea. Did it disappear? No:

dr4pl(data, dose, response)
## Error in dr4pl.default(dose = dose, response = response, init.parm = init.parm, : Both doses and responses should be numeric.

Looking at the data might give us an idea of the source:

str(data)
## Classes 'tbl_df', 'tbl' and 'data.frame':    10 obs. of  2 variables:
##  $ dose    : int  1 2 3 4 5 6 7 8 9 10
##  $ response: int  2 3 4 5 6 7 8 9 10 11

Both dose and response are integers. Might that be the problem?

data <- tibble(dose= (1:10)*1.1, response = (2:11)*1.1)
dr4pl(data, dose, response)
## Error in dr4pl.default(dose = dose, response = response, init.parm = init.parm, : Both doses and responses should be numeric.

Nope. Both dose and response are now definitely numeric:

str(data)
## Classes 'tbl_df', 'tbl' and 'data.frame':    10 obs. of  2 variables:
##  $ dose    : num  1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 11
##  $ response: num  2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 11 12.1

But the problem persists.

Might this be the reason?

head(data,2)
## # A tibble: 2 x 2
##    dose response
##   <dbl>    <dbl>
## 1   1.1      2.2
## 2   2.2      3.3

It is a tibble. And the variables are reported to be doubles.

But according to the documentation:

“numeric is identical to double (and real)”

That should not be a problem then.

However. In desperation, I tried this:

c <- data %>% 
  as.data.frame() %>% 
  dr4pl(dose, response)

And it works!

Why? Absolutely no idea!

Or do i?

The problem is that subsetting a tibble returns a new tibble. Well, subsetting a dataframe returns a dataframe as well?

It does. Unless you subset out a single variable or observation.

In a dataframe, df$var returns a vektor, containing the values of var in df.

If, however, df is a tibble, df$var will return a tibble, with just one variable.

Project Euler 61

Project Euler 61

OK, this was a bit of a challenge.

Find six four-digit numbers that are cyclical. As in: The last two digits of the first number is equal to the first two digits of the second number, the last two digits for the second number is equal to the first two digits in the second number. Etc. The last two digits in the final sixth number must be equal to the first two digits in the first number.

Also, one number must be triangle, one square, one pentagonal, one hexagonal, one heptagonal and on octagonal.

Lets begin by making lists of the different kinds of numbers:

Four-digit triangle-numbers:

P3 <- function(x){
  x*(x+1)/2
}
L3 <- P3(45:140)

Square numbers:

P4 <- function(x){
  x*x
}
L4 <- P4(32:99)

Pentagonal:

P5 <- function(x){
  x*(3*x -1)/2
}
L5 <- P5(26:81)

Hexagonal

P6 <- function(x){
  x*(2*x-1)
}
L6 <- P6(23:70)

Heptagonal:

P7 <- function(x){
  x*(5*x-3)/2
}
L7 <- P7(21:63)

Octagonal:

P8 <- function(x){
  x*(3*x-2)
}
L8 <- P8(19:58)

Lets get them all into a list:

values <- list(L8, L7, L6, L5, L4, L3)

And this is where it gets complicated.

I take each value in L8, which is now values[[1]]

For each of them, I run through each of the other elements of values, 2:6.

For each values[[i]], I check if there are any of the numbers, where the first two digits is equal to the last two in the element in L8 I’ve come to.

If there is, I run through all of them (the ones where the first two digits is equal to the last two in the element in L8 I’ve come to.)

For each of these, I run through each of the remaining list elements, setdiff(2:6, b), where b is the iterator in 2:6

For each of those, I check that there are numbers where the first two digits etc etc etc.

When I get to the last number, I check that the last two digits are the same as the first two digits in the number in the first loop. Then I sum the numbers I’ve found, and get the answer.

It is not difficult. As such. But there are a lot of nested for-loops, and I have to check that there is a possible next number, and, if not, go to the next element.

It took quite a lot of time to get it right…

for(i in values[[1]]){
  for(b in 2:6){
    if(length(values[[b]][(values[[b]] %/% 100) == (i %% 100)])==0){
        next()
      }
    for(j in values[[b]][(values[[b]] %/% 100) == (i %% 100)]){
      for(c in setdiff(2:6, b)){
       if(length( values[[c]][(values[[c]] %/% 100) == (j %% 100)]  )==0){
         next()
       }
       for(k in values[[c]][(values[[c]] %/% 100) == (j %% 100)]){
         for(d in setdiff(2:6, c(b,c))){
           if(length( values[[d]][(values[[d]] %/% 100) == (k %% 100)]  )==0){
         next()
       }
           for(l in values[[d]][(values[[d]] %/% 100) == (k %% 100)]){
             for(e in setdiff(2:6, c(b,c,d))){
               if(length( values[[e]][(values[[e]] %/% 100) == (l %% 100)]  )==0){
         next()
       }
               for(m in values[[e]][(values[[e]] %/% 100) == (l %%100)]){
                  for(f in setdiff(2:6, c(b,c,d,e))){
                    if(length( values[[f]][(values[[f]] %/% 100) == (m %% 100)]  )==0){
                    next()
                     } else{
                    for(n in values[[f]][(values[[f]] %/% 100) == (m %% 100)]){
                          if((n %% 100) == (i %/% 100)){
                        answer <- sum(i,j,k,l,m,n)
                        break()
                      }}
                    }
                  }   
               }
             } 

           }
          }
       }
     }
    }
  }
}

Neat. But there must be an easier way.

Statistik er svært

Eller, det er svært at blive enige om. Eller også er det svært at acceptere konsekvenserne.

Danmark er, ifølge Wikipedia, 86,3% etnisk dansk. I 1980 var tallet 97%. Så hvis magthaverne, virksomheds- og organisationslederne etc. i Danmark overvejende er hvide DJØF-mænd, så bør det ikke overraske. Jo, det med mænd. Det er skævt. Men at ledende stillinger overvejende er besat af personer der har taget en uddannelse der er målrettet de stillinger, bør ikke overraske. Og at de overvejende er hvide, i et land der stadig er overvejende hvidt. Det bør slet ikke overraske.

De personer der sidder i disse ledende stillinger, begyndte den uddannelse der ledte til stillingen for 30-40 år siden. Så ledende stillinger i Danmark er besat med personer der nogenlunde bør afspejle befolkningssammensætningen for 30-40 år siden. Var den hvid? Ja, det var den. Hvis man ønsker en anden sammensætning af hudfarver på de ledende poster i Danmark, må man arbejde på at ændre sammensætningen af hudfarver i Danmark i dag, og så tålmodigt vente på at de bliver gamle nok, har fået uddannelse nok, og ancinnitet nok til at nå de ledende poster.

 

Racisme. Sexisme. Eksklusion. Diversitet

Det er aldrig ok at forskelsbehandle folk på grund af deres hudfarve.

Det er aldrig ok at forskelsbehandle folk på grund af deres køn.

Det er aldrig ok at forskelsbehandle folk på grund af deres alder.

Eller – undertiden er det. Hvis folks hud er blå, har de adgang til andre sundhedstilbud, end folk hvis hud har en normal farve. Kvinder skal også have betydeligt bedre adgang til screening for livmoderhalskræft end mænd. Og pensioner udbetales altså ikke til folk under en vis alder.

Men hvis man vil slå sig op som en inkluderende organisation, så går det ikke at udtale sig negativt om folk på grund af deres køn, alder eller hudfarve. Eller kønsidentitet. Eller seksuel orientering. Og en lang række andre ting.

Du må ikke udskamme folk på grund af deres hudfarve. Heller ikke når de er hvide. Du må ikke udskamme folk på grund af deres køn. Heller ikke når de er mænd. Du må ikke udskamme folk på grund af deres alder. Heller ikke når de er midaldrende. Du må ikke udskamme folk på grund af deres seksuelle orientering. Heller ikke når de er bøsser. Du må ikke udskamme folk på grund af deres kønsidentitet. Heller ikke når de er ciskønnede.

Du må i øvrigt heller ikke udskamme folk for at spise småkager og drikke rose-vin.

StarLog – The main character. Enterprise!

In what ways does the U.S.S. Enterprise function as a character, not just a vehicle in Star Trek? Does “she” have a personality? Do the other ships in the Star Trek universe have the same level of character development?

To be honest I do not think that the starships actually can be viewed as characters. Yes, Enterprise is named, gendered, and the “real” characters talk to her. And the computer responds. But as a general rule, the computer has no selfawareness, does not solve problems (only at the prompt of the real characters), and only has a personality to the extent that the real characters project their own perceptions and ideas on to the ship.

We like to talk about Enterprise as a character. And Star Trek would not have been Star Trek without Enterprise. But I flatly dismiss the idea that Enterprise is actually a character in her own right.

StarLog – the future of propulsion

Where do you think ion propulsion and future engine technology will take us? What are the dangers? Are there other applications?

It will take us further out in space! But where it will also take us is not places far out in space. But also to more local spaces. Solving problems related to it, will give us new technology. And we have no idea where that will take us, in the same way that we did not know how the general theory of relativity and quantum mechanics would bring us GPS-navigation.

The dangers? Hard to say. I would claim that we should be mindful of not using all our reserves of Xenon for this. Other than that. Probably none. Unless it turns out that ion propulsion damages subspace in some way.

Finally. All four pips:

Mobning og asymmetriske magtforhold

Eller – hvad gør man når det er en leder der mobber. Og den mobbedes reaktion bruges som undskyldning for yderligere mobning?

Tja. Man søger nok andet arbejde. Hvis resten af ledergruppen synes det er helt i orden, er der tale om en organisation der er så dysfunktionel, at der næppe er meget at stille op.

Starlog – diversity

More homework for Star Trek: Inspiring Culture and Technology.

Why is it important to see yourself on television? Why is television an important subject for scholarly study and how does what we watch shape the world we live in?

Also:

Scott asks if you think we’re getting closer to realizing the Vulcan philosophy of IDIC (Infinite Diversity in Infinite Combinations) here on Earth. What would it take for that to happen? What would it look like? How might things be different?

The first question. Well, Scott answers it. It is important to see yourself on Star Trek. Because it shows a vision of a future. That has room for people like me.

I know it from myself. Seeing Jadzia Dax kiss her former wife in rejoined was important. It was not a lesbian kiss as such. And yet it was, and as a gay man, that actually meant something. Especially since we had waited for so long to actually see any representation of gay and lesbian characters in Star Trek, a thing Roddenberry had promised would be adressed in season five of TNG.

This makes a difference. We all to a certain degree view ourself through the stories we are told or shown. Small girls (and some boys) imagine themselves to be princesses when they are read fairytales. Grown men (and some women) imagine themselves to be action heroes when they watch Die Hard. And seeing yourself, or someone like you, portrayed positively makes a huge difference.

That is the real true promise of Star Trek. That the diversity we have on Earth today – not always ideal, will live on in the future, in a more positive and meaningful way, than it does for young people. No matter what their circumstances. In that words of Dan Savage, that it will get better.

That Vulcan ideal, Infinite Diversity in Infinite Combinations, is still in the future. But not as far off, as it has been. What it would take to get closer? I could come with a lot of politically correct suggestions about educating the ignorant, and fighting racism. But I think the underlying problem is scarcity, and a clash of cultures. Make sure that immigration does not mean that I have to pay more in taxes to support unemployed immigrants, and that they do not threaten my livelyhood by putting pressure on wages. And make sure that people of all cultures, accept the basic foundation of a liberal (in the original sense, not necessarily the american political sense) society, democracy, equality of the sexes, and non-discrimination, I think we will be all right. Not that we won’t still have idiots discriminating. But we should try to move to a point where idiots cannot get away with justifying their discrimination with religion and/or culture.

If we can do that, and despite bumps on the road, we are getting ever closer, we will be able to realise the alien ideal of IDIC.

Maybe that also explains why television makes sense as a subject of scholarly study. Television is one of the most important common representations of popular culture. We are placed in front of this entertainment for an inordinate amount of time every day. It affects a lot of people, in diverse ways. If that should not be an important subject of study, I don’t know what should be.

And that gives me the rank I would really like. Commander:

StarLog – we are explorers

At the end of the video, Margaret says that space exploration was controversial in the 1970s and 1980s. People wondered why the government was spending time and money exploring the solar system when critical problems existed here on Earth. What do you think? Should the government resolve Earthly issues before exploring space? Or is a scientific investigation of distant worlds a fundamentally human endeavor of exploration? Explain your argument.

No. Governments should not attempt to solve all problems on the globe, before exploring. One. Human life is one continous series of problems and challenges. We will never get past the goal-post, because it will continously move. It is an impossible goal.

Should we not try anyway? Why pour money into a spaceprogram, when we could instead help homeless people, to take just one of the, relatively, smaller problems we are facing? It is hard to argue for exploration, especially in an endeavour as expensive as spaceexploratoin, in the face of a homeless man.

But humanity is fundamentally an exploring race. The reason we are everywhere on this planet, for good and bad, is that we have explored. The reason that life-expectancy is at a record high, even in poor countries in the third world, is that we are explorers. The introductory video mentions that. We are explorers, not only in a geographical, or astronomical sense, but in every sense of the word. Abandoning the exploration, even the expensive space-version of it, betrays our future. To take just one very basic example, the images brought home from the moon, for the first time showing how small, beautiful and fragile our planet is, made it clear to humanity how extraordinary lucky we are to live. And how important it is to take care of the one planet in the universe where we know live exist.

Now we just need to prove that intelligent live exists somewhere in the universe. Demonstrating that it exists on Earth, would be a good place to start.