Have a screw loose
To be mad or insane is the opposite of having one’s head screwed on the right way, both metaphors date from the late 18th/early 19th century, hence, screwy, which dates from a little later.