Apparatus and methods are described, including apparatus for treating a subject for obstructive sleep apnea (OSA). The apparatus includes an implant, an imaging device, and a control unit. In response to a set of training images received from the imaging device, the control unit learns an association between a position of a head of the subject and OSA of the subject. In response to the association, the control unit establishes, for an OSA-related parameter, at least two distinct thresholds corresponding to respective different head positions. Subsequently, at a second time, the control unit selects a threshold that corresponds to the position of the subjects head, and if the value of the OSA-related parameter passes the selected threshold, treats the subject for OSA by driving the implant to apply current to an anatomical site of the subject. Other applications are also described.